0

Is this means assignment in js of primitive makes primitive become object automatically? let a = 1 just be transferred to let a = new Number(1), we know Number is a function, and it's prototype has toString, this makes sense? Is it right?

Finally, we know the primitive of js is stored in stack memory, but if we can only get object by assignment, so is it means only pointer exists in stack? I am confused. Thanks for your answers if you can help me.

CodeAlien
  • 766
  • 2
  • 7
  • 15
  • This is a limitation of the JavaScript syntax. It's not the case in something like Ruby. – tadman Oct 23 '20 at 01:30
  • 1
    "the primitive of js is stored in stack memory" is not necessarily true. It may not exist at all if it can be optimized away during compilation, or it might never hit the stack if the code is just evaluated as parsed. – tadman Oct 23 '20 at 01:31
  • 1
    You can always wrap the number in parentheses, if you really want. `(1).toString()`. The style you want is not allowed just because of parsing ambiguities. – Scott Sauyet Oct 23 '20 at 01:32
  • 2
    The token-level syntax interprets `.` as a decimal point. That overrules the meaning of `.` as an object property reference. That's why the enclosing `( )` are needed. – Pointy Oct 23 '20 at 01:33
  • you can use (1).toString() instead – Quang Phu Oct 23 '20 at 01:37
  • i just find the same question you need https://stackoverflow.com/a/40409939/14404566, give it a check – Quang Phu Oct 23 '20 at 01:39
  • So there isn't real primitive data in JS? We can declare `int a = 1;` in Java or C, this is primitive, we cannot use any function of it. But it isn't exist in JS ? If we just declare a `1 or 'string'`, there isn't any variable point it, it's nothing. – CodeAlien Oct 23 '20 at 01:53
  • @CodeAlien Primitives just get automatically wrapped when you call a method on them. But this has nothing to do with the question about `1. toString` being invalid syntax. – Bergi Oct 23 '20 at 01:59
  • @Bergi thx for your answers, I searched, found it is called autoboxing. But I cannot find whether this autoboxing affect the original variable. Like if `let a = 1; a.toString();` whether `a` become` Number(1) `? or after `a.toString()` the Number(1) is just cleared by GC? – CodeAlien Oct 23 '20 at 02:17
  • @CodeAlien No, the variable `a` is not affected, autoboxing happens on the value only. The boxed value is pretty ephemeral, it is used only to look up the property (method inherited from the respective prototype) and nothing else (it's not even being passed as `this` to the method, the original primitive value is). In fact it's so short-lived that engines completely optimise it away and never instantiate it in the first place. – Bergi Oct 23 '20 at 02:56
  • @Bergi Thx for your answers, But when I tried the code below. If `a` is not affected, how can the code run correctly? ``` let a = 1 a.__proto__.test = () => { console.log('test') } a.test(); // correct ``` – CodeAlien Oct 25 '20 at 03:03
  • 1
    @CodeAlien Because you're affecting `Number.prototype` there. Not `a`. – Bergi Oct 25 '20 at 05:26

1 Answers1

1

You can not call Number.prototype methods directly from a number in digits form, e.g. 1 2 3 4 5 6 7 8 9, you have to wrap them in parentheses.

// This doesn't work
console.log(1.toString());

// This works
console.log((1).toString());
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34