Created
May 23, 2012 20:57
-
-
Save pbouchet/2777770 to your computer and use it in GitHub Desktop.
Closures
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
** Scheme ** | |
(define makeaccount | |
(lambda (balance) | |
(lambda (amt) | |
(begin (set! balance (+ balance amt)) | |
balance)))) | |
<==> | |
** JavaScript ** | |
var makeAccount = function(balance) { | |
return function(amt) { | |
balance += amt; // since balance is in the local scope, this function will close around it | |
return balance; // even though it hasn't been assigned to a var (see jsfiddle.net/6g5U6). | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment