I want to be able to have two references to the same primitive value, and any change done via one should be reflected to the other "magically" - i.e. using C code as an example:
// (C code):
int value = 0;
int *p1 = &value;
...
int *p2 = &value;
*p2 = 1;
...
printf("%d", *p1); // gives 1, not 0
The only way I've figured it out so far is by using an extra object indirection:
var a = { valueWrapper: { num: 1, str: 'initial' } };
var b = a;
// change a
a.valueWrapper.num = 2;
a.valueWrapper.str = 'changed';
// show b
console.log(b.valueWrapper.num);
console.log(b.valueWrapper.str);
// outputs:
//
// $ node test.js
// 2
// changed
Is there a cleaner way?