Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behnood-eghbali/4a7c27b0d8cd00b6a71a7c17cd6d0e5d to your computer and use it in GitHub Desktop.
Save behnood-eghbali/4a7c27b0d8cd00b6a71a7c17cd6d0e5d to your computer and use it in GitHub Desktop.
const getSecret = (secret) => {
return {
get: () => secret
};
};
test('Closure for object privacy.', assert => {
const msg = '.get() should have access to the closure.';
const expected = 1;
const obj = getSecret(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();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment