11
When we do the following test below, it is returned false
.
console.log(7 instanceof Number); // FALSE
However, in the second test, it is returned true
.
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 this, since, in the examples below, both the object
like the number
shall have the method established in Number
through the prototype
!
Behold:
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 happens craziness variation?
Perhaps because 7(or any other number) is treated as a string when it is not declared?
– MarceloBoni
Sorry now I understood better, I will finish the answer and already put
– SneepS NinjA
@Marcelobonifazio,
typeof 7 = 'number'
.typeof '7' = 'string'
– Wallace Maxters
It is not only 7 that is not an instance of Number.. Its title is still half way misleading
– Math
When someone gives a -1, could you at least explain , to see what I can improve on my question
– Wallace Maxters
need to explicitly instantiate...
– Daniel Omine
@Wallacemaxters I took -2 and no one explained it to me either, but the day I’m going through today... what least worries me is the negatives here
– SneepS NinjA