Number() itself returns a numeric primitive. When you call new Number() , you get a new instance of the object that represents Number ( corresponding to the ES5 specification ).
When you call a property in a primitive, the primitive is automatically placed in a window (for example, in Java) into an instance of this object, which allows you to call helloWorld() on an object or Number .
However, try this;
var x = Number(5); x.bar = function (x) { alert(x); }; x.bar("hello"); var y = new Number(5); y.bar = function (x) { alert(x); }; y.bar("hello");
You will see that the latter works while the first does not work; in the first, Number autoboxing to the number, and the bar method (object) is added to it. When you call x.bar() , you create a new number with an automatic number that bar does not exist.
In the latter case, you add the bar method to the Number instance, which behaves like any other instance of the object, and therefore it is preserved throughout the entire life cycle of the object.
Matt
source share