Why do literal variables in JS have methods?

var num = 4;
num.toString();// 4
var nnum = new Number(4)
nnum.toString(); // 4

clipboard.png

clipboard.png

numnnum

clipboard.png
but it"s different here. How do you do that?

Mar.31,2021

I have read your comments on other answers, and you would like to see the more authoritative ECMAScript specification:

num.toString () is an attribute access operator Property Accessors, specific specification in https://www.ecma-internationa. is classified as

MemberExpression. IdentifierName

and the MemberExpression specification is listed in https://www.ecma-internationa., but in this case, it belongs to

.
PrimaryExpression

We continue to trace that the PrimaryExpression, specification is defined in https://www.ecma-internationa.

.

and num belongs to IdentifierReference

if you write 10.36.toString () , it belongs to Literal.

and (10). ToString () , (10) is a parenthetical expression.

< hr >

so in num.toString () , num belongs to MemberExpression, toString belongs to IdentifierName, . is Property Accessors .

  1. Let baseReference be the result of evaluating MemberExpression.
  2. Let baseValue be? GetValue (baseReference).
  3. Let bv be? RequireObjectCoercible (baseValue).
  4. Let propertyNameString be StringValue of IdentifierName.
  5. If the code matched by this MemberExpression is strict mode code, let strict be true, else let strict be false.
  6. Return a value of type Reference whose base value component is bv, whose referenced name component is propertyNameString, and whose strict reference flag is strict.

see the RequireObjectCoercible above? That's the answer you want.


Auto-boxing to learn about


  

basic types are converted to wrapper types during point operations. That's why everything is object in JS

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b36b66-2c02b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b36b66-2c02b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?