Skip to content

Instantly share code, notes, and snippets.

@kampfer
Created March 6, 2013 07:21
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 kampfer/5097383 to your computer and use it in GitHub Desktop.
Save kampfer/5097383 to your computer and use it in GitHub Desktop.
a example about closure
function decorate(fn, decorator) {
return function() {
var args = Array.prototype.slice.apply(arguments);
args.unshift(fn);
return decorator.apply(null, args);
}
}
function total(x, y) {
return x + y;
}
function ph(fn, x, y) {
var n = fn.call(null, x, y);
return n * n;
}
var pp = decorate(total,ph);
console.log( total(3, 5) ); //8
console.log( pp(3, 5) ); //64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment