Skip to content

Instantly share code, notes, and snippets.

@nowk
Created April 14, 2014 15:47
Show Gist options
  • Save nowk/10659909 to your computer and use it in GitHub Desktop.
Save nowk/10659909 to your computer and use it in GitHub Desktop.
Javascript Closure practice
/* 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