Skip to content

Instantly share code, notes, and snippets.

@XuefengWu
Last active January 3, 2016 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XuefengWu/8393628 to your computer and use it in GitHub Desktop.
Save XuefengWu/8393628 to your computer and use it in GitHub Desktop.
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