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?