Last active
January 3, 2016 02:09
-
-
Save XuefengWu/8393628 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add(x) {return function(y) {return x + y;}} | |
var add5 = add(5) | |
add5(1) | |
//log, warn,info,notice | |
function counter() {var count = 0; return function() {count ++; return count;}} | |
var increment = counter() | |
increment() //1 | |
increment() //2 | |
//ajax | |
function partial(fn /*, args...*/) { | |
// A reference to the Array#slice method. | |
var slice = Array.prototype.slice; | |
// Convert arguments object to an array, removing the first argument. | |
var args = slice.call(arguments, 1); | |
return function() { | |
// Invoke the originally-specified function, passing in all originally- | |
// specified arguments, followed by any just-specified arguments. | |
return fn.apply(this, args.concat(slice.call(arguments, 0))); | |
}; | |
} | |
function addAllTheThings() { | |
var sum = 0; | |
for (var i = 0; i < arguments.length; i++) { | |
sum += arguments[i]; | |
} | |
return sum; | |
} | |
var addFromSix = partial(addAllTheThings, 1,2,3); | |
addFromSix(4) //10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment