Created
April 14, 2014 15:47
-
-
Save nowk/10659909 to your computer and use it in GitHub Desktop.
Javascript Closure practice
This file contains hidden or 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
/* jshint node: true */ | |
var assert = require("assert"); | |
/* | |
* fn | |
* | |
* @return {Function} | |
*/ | |
function fn() { | |
var i = 0; | |
return function(val) { | |
if (val) { | |
i = val; | |
} else { | |
return i; | |
} | |
}; | |
} | |
describe("closures", function() { | |
it("has isolated scopes", function() { | |
var a = fn(); | |
var b = fn(); | |
a(1); | |
b(2); | |
assert(a() === 1); | |
assert(b() === 2); | |
b(3); | |
assert(a() !== 3); | |
assert(a() === 1); | |
a(4); | |
assert(b() !== 4); | |
assert(b() === 3); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment