Skip to content

Instantly share code, notes, and snippets.

@ericelliott
Last active October 26, 2021 13:46
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ericelliott/2b624243f030405abe0c to your computer and use it in GitHub Desktop.
Save ericelliott/2b624243f030405abe0c 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();
});
@jonnygamba
Copy link

jonnygamba commented Jun 13, 2017

@ericelliott shoudln't L12 be secret instead of actual?

@ericelliott
Copy link
Author

No, the point is to demonstrate that you can't access secret from outside getSecret().

@jonnygamba
Copy link

@ericelliott got it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment