Last active
October 26, 2021 13:46
-
-
Save ericelliott/2b624243f030405abe0c to your computer and use it in GitHub Desktop.
Closure for data privacy: https://jsbin.com/gareno/edit?html,js,output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
@ericelliott got it 👍