Skip to content

Instantly share code, notes, and snippets.

@manjeettahkur
Forked from nowk/closure.js
Created June 4, 2016 13:50
Show Gist options
  • Save manjeettahkur/78b94a5a41e03b248a0662a6679f6c91 to your computer and use it in GitHub Desktop.
Save manjeettahkur/78b94a5a41e03b248a0662a6679f6c91 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