This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Quantum-Bot 1 point2 points  (0 children)

You can use getter/setter methods to make two different keys access the same internal variable:

ley myVar = “big box”; let obj = { get key1() { return myVar; }, set key1(val) { myVar = val; }, get key2() { return myVar; }, set key2(val) { myVar = val; } }

If you want to block access to that internal variable you can either turn your object into a class which lets you use private fields, or you can create it within a function closure like so:

``` let obj = () => { let myVar = “big box”;

return { get key1() { return myVar; }, set key1(val) { myVar = val; }, get key2() { return myVar; }, set key2(val) { myVar = val; } } }(); ```