I am following a JavaScript tutorial on W3Schools. While reading almost on each page they give note to user to \"Avoid creating objects\" and to use primitive data types ins
It changes the intuitive way the operators behave with numbers, strings and booleans:
===
) breaks when any of the numbers is constructed, so 42 === 42
is true, while 42 === new Number(42)
is not,==
) breaks when both numbers are objects, so 42 == new Number(42)
is true, while new Number(42) == new Number(42)
is not,typeof
operator gives different result when a number is constructed, so typeof(42)
is number
, but typeof(new Number(42))
is object
,0
is false, but new Number(0)
is true, so the following two will have different behavior:var a = 0;
if (a)
console.log("not zero");
else
console.log("zero!"); // "zero!"
var b = new Number(0);
if (b)
console.log("not zero"); // "not zero"
else
console.log("zero!");
So, avoid new Number
, new String
and new Boolean
.
Apart from that, there is the issue of using / not using new
with constructors. It stems from several facts:
this.foo
syntax to add new properties and methods;new
keyword, this
becomes the global object, leading to side effects.As a result, a tiny mistake can have catastrophic effects:
color = "blue";
var Fruit = function(color) {
this.color = color;
return this;
};
var apple = new Fruit("green");
console.log(apple.color); // "green" -- okay
console.log(color); // "blue" -- okay
var banana = Fruit("yellow");
console.log(banana.color); // "yellow" -- okay
console.log(color); // "yellow" -- wait, what?
console.log(banana.apple); // "{ color: 'green' }" -- what??
console.log(banana.document); // "{ location: [Getter/Setter] }" -- what???
(That's why some people resort to adding explicit checks in the constructor, or using closures instead. But that's for another story.)