javascript curry 柯里化函数 仿lodash的curry

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>curry</title>
</head>
<body>
<script>
	function curry(fn) {
		var  _argLen = fn.length

		function wrap() {
			var _args = [].slice.call(arguments)
			function act() {
				_args = _args.concat([].slice.call(arguments))
				if(_args.length === _argLen) {
					return fn.apply(null, _args)
				}
				return arguments.callee
			}
			if(_args.length === _argLen) {
					return fn.apply(null, _args)
			}

			act.toString = function() {
				return fn.toString()
			}
			return act
		}

		return wrap
	}

	var abc = function(a, b, c) {
		return [a, b, c]
	}

	var curried = curry(abc)
	
	console.log(curried(1)(2)(3))
	// => [1, 2, 3]

	console.log(curried(1, 2, 3))
	// => [1, 2, 3]

	console.log(curried(1, 2)(3))
	// => [1, 2, 3]

	console.log(curried(1)(2, 3))
	// => [1, 2, 3]

</script>
</body>
</html>
posted @ 2017-05-04 16:31  不要相信我  阅读(1193)  评论(0编辑  收藏  举报