Calling Javascript Factory Method
Solution 1:
In normal cases, new
should be used when creating objects from a constructor function and eschewed when performing any other function call. When using new
on a function 1) a new object is created; 2) the function is called with the value of this
bound to that new object, and 3) the value returned from the function (by default) is the object created in step one.
However, in your case you're returning a completely new object from the "constructor" (different from the object in 1) above), which means there is no practical difference. The value of this
will still be different inside the function, but both of these will return false
:
new Foo() instanceof Foo;
Foo() instanceof Foo;
To illustrate the this
difference, add the following to Foo
:
alert(thisinstanceof Foo)
When called with new
this alerts true
; when called without it, false
.
Furthermore, there's no point in assigning an object to Foo.prototype
because you'll never create any instances of Foo
that would make use of it (because, again, you're returning something completely different from Foo
).
If you're going to return a custom object from Foo
then you should prefer the version without new
; there's no point in creating a new instance of Foo
if you're going to ignore it and return something completely different, anyway.
Solution 2:
Here's a great link:
Is Javascript "new" considered harmful?
See also:
http://phabricator.com/docs/phabricator/article/Javascript_Pitfalls.html
But here's the best link (and, really, the answer to your question:
http://www.crockford.com/javascript/inheritance.html
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This can be puzzling to programmers trained in conventional object-oriented languages like C++ and Java...
Solution 3:
The javascript new keyword in functions is pretty weird. The best (only?) in depth explanation of what is happening I've been able to find is actually this SO answer from Daniel Howard.
Should be required reading for anyone trying to learn some of the more advanced concepts of Javavscript.
By the way, you can get really far in js by never using the new keyword at all. Notice how basically none of the code using jquery relies on it.
Solution 4:
Because you are returning an object from Foo
, the use of new
has no effect. The return values of Foo()
and new Foo()
will not be instances of Foo
; instead, they'll be instances of the type of whatever value you returned.
This same behavior is seen if you return any† object or any function from your constructor. However, if Foo
were to return:
- A string
- A number
null
undefined
(same as returning nothing at all)- or
this
...then the return value of new Foo()
would be an instance of Foo
.
You can test out the code below on JSFiddle:
functionReturnsString() { return"string"; }
functionReturnsNumber() { return1; }
functionReturnsNull() { returnnull; }
functionReturnsUndefined() { returnvoid0; }
functionReturnsThis() { returnthis; }
functionReturnsFunction() { returnfunction() {}; }
functionReturnsObject() { return {}; }
alert("Results:"
+ "\nReturnsString: " + (newReturnsString() instanceofReturnsString)
+ "\nReturnsNumber: " + (newReturnsNumber() instanceofReturnsNumber)
+ "\nReturnsNull: " + (newReturnsNull() instanceofReturnsNull)
+ "\nReturnsUndefined: " + (newReturnsUndefined() instanceofReturnsUndefined)
+ "\nReturnsThis: " + (newReturnsThis() instanceofReturnsThis)
+ "\nReturnsFunction: " + (newReturnsFunction() instanceofReturnsFunction)
+ "\nReturnsObject: " + (newReturnsObject() instanceofReturnsObject)
);
The ReturnsFunction
and ReturnsObject
constructors will both show a false
result, meaning they don't return an instanceof
themselves when used with the new
operator.
All of the other constructors do return an instanceof
themselves when used with the new
operator.
† Except this
, as I mentioned above
Post a Comment for "Calling Javascript Factory Method"