I’m trying to implement the node.js module.exports and exports rule in a script. These 2 variables should be equal to each other at all times. Even when one changes the other one should change too.
I tried doing this with setters, but both o.foo and o.bar end up undefined =/
var foo = { hello: 'world' };
var o = {
foo: foo,
bar: foo
};
var inuse = false;
var setter = function(val) {
if (inuse) return;
inuse = true;
o.foo = val;
o.bar = val;
inuse = false;
};
o.__defineSetter__('foo', setter);
o.__defineSetter__('bar', setter);
o.foo = { hey: 'there' };
console.log(o.foo);
console.log(o.bar);
Create getters as well, and assign to different property names.
DEMO: http://jsfiddle.net/jqExh/
Or use variables instead of properties:
DEMO: http://jsfiddle.net/jqExh/1/
This way the only way to get and set them is via the accessors.