I am reading YDKJS series Types and Grammar. And here is one snippet that the author is using:
function foo(x) {
x = x + 1;
x; // 3
}
var a = 2;
var b = new Number( a ); // or equivalently `Object(a)`
foo( b );
console.log( b ); // 2, not 3
I have tried change the line
var b = new Number(a);
to
var b = Object(a);
and
var b = a;
But the result remains the same.
Here is the explanation the author gives out :
The problem is that the underlying scalar primitive value is not mutable (same goes for String and Boolean). If a Number object holds the scalar primitive value 2, that exact Number object can never be changed to hold another value; you can only create a whole new Number object with a different value.
When x is used in the expression x + 1, the underlying scalar primitive value 2 is unboxed (extracted) from the Number object automatically, so the line x = x + 1 very subtly changes x from being a shared reference to the Number object, to just holding the scalar primitive value 3 as a result of the addition operation 2 + 1. Therefore, b on the outside still references the original unmodified/immutable Number object holding the value 2.
Can someone explain it in a more direct way (especially the second paragraph)? I don’t understand these 2 paragraph.
If all the primitive values are not changeable, then
var a = 2;
a++;
a;
should give you 2 right?