In JavaScript, I was wondering if there is anything special about new or if it is just syntactic sugar for call(). If I have a constructor like:
function Person ( name, age ){
this.name = name;
this.age = age;
}
is
var bob = new Person( "Bob", 55 );
any different than
var bob;
Person.call( bob = new Object(), "Bob", 55 );
?
They are not equivalent in your example, because
bobdoes not inherit fromPerson.prototype(it directly inherits fromObject.prototype).The equivalent version would be
Is it syntactic sugar? Might depend on how you define it.
Object.createwas not available in earlier JS versions, so it was not possible to set up object inheritance withoutnew(you are able to overwrite the internal__proto__property of an object in some browsers, but that is really bad practice).As a reference, how
newworks is defined in http://es5.github.com/#x11.2.2 and http://es5.github.com/#x13.2.2.Object.createis described in http://es5.github.com/#x15.2.3.5.