Created
March 6, 2013 07:21
-
-
Save kampfer/5097383 to your computer and use it in GitHub Desktop.
a example about closure
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 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