Primitive types are immutable, so no, this is not possible. You can transfer your primitive type to an object, for example:
function MyNumber(n) { this.n = n; }
MyNumber.prototype.valueOf = function() { return this.n; }
var someObject = { a: 1, b: new MyNumber(2) };
var myRef = someObject.b;
MyNumber.call(myRef, myRef + 1);
console.log(+someObject.b);
OR
var someObject = {
a: { value: 1 },
b: { value: 2 },
};
var myRef = someObject.b;
my_inc(myRef);
The React Framework uses a very simple template to encapsulate values.
function Link(value, requestChange)
{
this.value = value;
this.requestChange = requestChange;
}
You can bypass the object, the current value can be accessed by checking the value of the property of the object, if you want to change it, you can call requestChangewith a new value, you can change the value. The advantage would be to have an actual “storage location” and logic for changing the value separate from access to read and write values. Note that values can also be complex objects.
You can also achieve something similar with closure:
var someObject = {
a: 1,
b: 2
};
function property(object, prop) {
return {
get value () {
return object[prop]
},
set value (val) {
object[prop] = val;
}
};
}
var ref = property(someObject, "b");
ref.value;
++ref.value;
someObject.b;
, getter setter , (object prop). ref, ..