function foo() {
return {a:9};
}
var bar = foo();
bar.a //returns 9
or
function foo() {
return {a:9};
}
var bar = new foo();
bar.a // returns 9
As far as I know new isnt used with the object literal notation, but how come new does work with it? Also, why is it that prototype can be accessed with new object, but not with the object literal notation?
edit:
I understand it now, if anyone else who stumbles upon this problem/question this might help you understand it:
function foo() {
this.a = "LOL";
return "WTF";
};
var bar = new foo(); // bar = [Object object]
var c = foo(); // c = "WTF"
window.a // "LOL"
Also read the accepted answer.
Using
newin this circumstance has nothing to do with what you return, but whatthisis inside offoo(). Usingnew,thiswill be a reference to a foo instance inside offoo(). Without new,thiswill be the global (probablywindow)