Possible Duplicate:
What is the 'new' keyword in JavaScript?
creating objects from JS closure: should i use the “new” keyword?
See this code:
function friend(name) {
return { name: name };
}
var f1 = friend('aa');
var f2 = new friend('aa');
alert(f1.name); // -> 'aa'
alert(f2.name); // -> 'aa'
What's the difference between f1
and f2
?
The new in your case isn't usefull. You only need to use the new keyword when the function uses the 'this' keyword.
function f(){
this.a;
}
// new is required.
var x = new f();
function f(){
return {
a:1
}
}
// new is not required.
var y = f();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With