Reputation: 1768
I was going though this presentation http://channel9.msdn.com/Events/MIX/MIX11/FRM08. Steve Anderson used a function having structure similar to the below.
function o(p) {
return { n: p };
}
To create an object from function o he used
new o(123)
I guess there is no need of new operator in this case. You can simply write.
o(123)
What would be the difference in the two ways?
Upvotes: 0
Views: 62
Reputation: 700342
The code doesn't make much sense. If the function is used without the new
keyword it creates and returns a single object. If it's used with the new
keyword, as in the example, two objects are created, and the object created inside the function is discarded.
If you return an object from the function, you should not use the new
keyword. A function that is supposed to be used with the new
keyword doesn't return an object, instead it uses the this
keyword to set properties in the already created object:
function o(p) {
this.n = p;
}
Upvotes: 2