Skip to content

Instantly share code, notes, and snippets.

@coto
Created January 8, 2016 17:29
Show Gist options
  • Save coto/7fcc34e41c7ba7c9e724 to your computer and use it in GitHub Desktop.
Save coto/7fcc34e41c7ba7c9e724 to your computer and use it in GitHub Desktop.
/*
interface Obj {
get() => Any;
}
o(secret: Any) => Obj
*/
const o = (secret) => {
return {
get: () => secret
};
};
test('Closure for object privacy.', assert => {
const msg = '.get() should have access to the closure.';
const expected = 1;
const obj = o(1);
const actual = obj.get();
try {
assert.ok(secret, 'This throws an error.');
} catch (e) {
assert.ok(true, `The secret var is only available
to privileged methods.`);
}
assert.equal(actual, expected, msg);
assert.end();
});
// Check on https://jsbin.com/midewovosu/edit?html,js,output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment