Why in JavaScript, 7 (a number) is not an instance of Number?

9

When we do the following test, we return false .

console.log(7 instanceof Number); // FALSE

However, in the second test, true is returned.

var number = new Number('3');

console.log(number instanceof Number) // TRUE

In a second scenario, we also have a variation when we use typeof :

typeof 1 // "number"

typeof new Number(1) // "object"

I do not understand why, as in the examples below, both object and number will have the method created in Number through prototype !

See:

Number.prototype.square = function ()
{
   return Math.pow(this, 2);
}

var x = 3;

x.square(); // 9

new Number(3).square() // 9

(3).square() // 9

Does anyone know why this doidice variation occurs?

    
asked by anonymous 20.07.2015 / 16:22

3 answers

13

In JavaScript, there are primitive types for String , Number and Boolean and Null and Undefined types, which only have one instance each). So, primitive values like the example below are not objects:

var texto = "Texto";
var numero = 7;
var booleano = true;

texto instanceof String;     // false
numero instanceof Number;    // false
booleano instanceof Boolean; // false

The instanceof operator does not say the value type . If the value is an object, instanceof says whether what is on the right is one of the prototype chain of that object - and therefore new Number(7) instanceof Number === true , since the number was created with the constructor Number .

What gives the value type is the typeof operator, with which you get the type of the primitives:

typeof texto;     // "string"
typeof numero;    // "number"
typeof booleano;  // "boolean"

When you try to access a property, the engine knows how to

20.07.2015 / 17:25
6

Strings and Numbers are primitive values, not objects and therefore do not have [[Prototype]] , so it will only work if you instantiate them as objects.

The same problem would occur in the case:

console.log("Texto" instanceof String); -> false

var texto = new String('3');
console.log(texto instanceof String) -> true

It has a very explanatory and good reference here in the OS

    
23.05.2017 / 14:37
4

Simply because it "is not" a object instance .

Object instances are declared using keyword new .

The reported problem also occurs for the following situations:

"string" instanceof String;      // false
true     instanceof Boolean;     // false

The array and object

({foo: "bar"}) instanceof Object;  // true
[1,2,3] instanceof Array;          // true

This is because objects actually are objects in javascript , as in the case of array is even more peculiar, since in Javascript arrays are objects, so much so that the following expression is true:

[1,2,3] instanceof Object; // true
typeof [1,2,3];            // object

Javascript is not (was) an object-oriented language, but rather a prototype-oriented language (or object prototyping). In MDN it reads as follows:

  

In Javascript inheritance occurs through prototype objects 4 and defines a a "is a" relationship. Each object inherits properties and methods from its prototype object that is referenced by the prototype property. MDN: Object Oriented Javascript

That is, in your example:

Number.prototype.square = function ()
{
   return Math.pow(this, 2);
}

You are adding a method to the prototype of the object Number , that is, not only Number 's objects will inherit this method, but also any "object" (typos and etc) that also inherit the Number .

Some links about:

20.07.2015 / 17:04