In another question, a user pointed out that the new
keyword was dangerous to use and proposed a solution to object creation that did not use new
.
new
isn't required and should be avoidedvar str = new String('asd'); // type: object
var str = String('asd'); // type: string
var num = new Number(12); // type: object
var num = Number(12); // type: number
new
is required, otherwise you'll get an errornew Date().getFullYear(); // correct, returns the current year, i.e. 2010
Date().getFullYear(); // invalid, returns an error