Why does a primitive variable act like an object?

If we add a method to the Number function (either a Boolean or String) like this

Number.prototype.sayMyNumber = function(){ return "My number is " + this; } 

and then create a number object, assign it to a variable

 var num1 = new Number(34); num1.sayMyNumber(); // it says "My number is 34" 

This is fine and expected since we created the Number object.

Similarly, if I create a primitive variable

 num2 = 34; num2.sayMyNumber(); // it says "My number is 34" 

Surprisingly, num2 also has a sayMyNumber () method, although it did not explicitly create a Number object.

Then I tried this,

 34.sayMyNumber(); // error, this does not work 

Why does num2 work?

Update

This is the next question I asked in the comments section, I put it here for better visibility.

The answers below mention that num2 is considered an internal "Number" object. This bothers me even more.

 typeof num1 === "number" // returns false typeof num2 === "number" // returns true typeof num1 === "object" // returns true typeof num2 === "object" // returns false 

Does this mean that num2 is not an "object"? If it is not an “object”, then how can it be an instance of “Number”?

+6
source share
3 answers

The primitive type Number has a corresponding representation of the object that you can create with new Number . This is an object and therefore has a different data type than the number of the primitive type.

If you call Number(34) (without new ), the object is not created, but the Number function converts the type to the value of a primitive number.

 var num1 = new Number(34); // object var num2 = 34; // primitive 

When you call sayMyNumber() on the primitive number num2 , JavaScript internally converts the primitive temporarily to its equivalent version of the object. And since you added sayMyNumber() to Number.prototype , you have access to this function.

Reason 34.sayMyNumber() does not work, because when the JavaScript engine parses the source code, it should only interpret what the dot means in this context. In the case of 34.sayMyNumber() point is potentially ambiguous. Does this mean a decimal separator? Or does it mean access to a member of an object? JavaScript allows you to interpret all integers, followed by a dot representing part of the floating point number. But since there aren’t 34.sayMyNumber (), it raises a SyntaxError. More here

+3
source

For both,

 var num1 = new Number(34); num2 = 34; 

You assign values ​​of type "Number" to the variables num1 and num2 . Therefore, he calls the prototype attached to "Number" .

But calling 34.sayMyNumber has a syntax error and its illegal operator.

If anyone has a question about this, contact ... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

-3
source

JavaScript encoding between primitives and objects. In this case, the numeric value is forced to bind to the Number object to gain access to the prototype of the sayMyNumber method. In fact, in this way primitives have access to all the properties and methods defined by their respective object constructors. However, they differ from objects because they are immutable.

Thus, (34).sayMyNumber(); works; so (34).toFixed() or (34).toPrecision() .

As for 34.sayMyNumber(); as stated earlier, this is a syntax error.

-3
source

All Articles