Is there any difference in how these functions operate? The first one is more typically of what I think about when thinking of a constructor.
Example 1: using this to name and set properties. Then using new to create a new Book object.
function Book(name, numPages) {
this.name = name;
this.numPages = numPages;
}
var myBook = new Book('A Good Book', '500 pages');
Example 2: returning a object by using new and just calling the function itself.
function Movie(name, numMinutes) {
return { name:name, numMinutes:numMinutes };
}
var best = new Movie('Forrest Gump', '150');
var other = Movie('Gladiator', '180');
I guess what I'm trying to figure out is if these are different in the way they create an object? If so is one better than the other? Are there different situations where one would work better over the other?
The difference is the constructor used to create the object returned.
creates a
Book
object instance, with the instance inheriting properties fromBook.prototype
, including aconstructor
property value ofBook
. TheBook.prototype
object itself inherits fromObject.prototype
.uses
Movie
as a factory function (new
not required) and returns an Object object instance, with the instance inheriting properties directly fromObject.prototype
, including aconstructor
property value ofObject
.More briefly stated, Object literal syntax creates an Object object.
Basically, when you use
new
, the JS engine makes a brand new object for you and injects that as the value ofthis
. It also automatically gives you any methods attach to the prototype of the constructor. Using a constructor also allows you to check if an object is aninstanceof
something more easily.Everything that
new
offers you can be attained through other methods but requires more manual labor.The second method, factories, tend to work better for unit testing, custom object creation and functional programming. It works better for unit testing because if you have a factory producing all of your objects, you can just replace that factory with a mock-up to test different cases.
As for when you use either, it all depends. Some people don't use
new
at all. Others exclusively usenew
. It all depends on if you need any of the things listed above, how much control you need over the creation of objects, when you want to usethis
or not, etc. In the end, it's all a matter of preference.The first one is a constructor, and can therefore be extended by a
prototype
, and you can test viainstanceof
wether the result is an Instance of this type. Downside: if you forget thenew
-keyword your code will blow up (unless you write a workaround for that into eachconstuctor
)And you can't really use
apply()
with a constructor to pass an array of arguments, when you instantiate a new Object; on the other hand, don't do that, even if you can/could.The second one is a factory, not a constructor. Independant wether you use the
new
-keyword or not. with this implementation it creates Objects that look the same but don't share a type or prototype (although the underlying JS-engine recognizes them as similar and so they share the same hidden Class as long as they have the same properties, added in the same order, ... different topic)long story short, neither performance nor memory-footprint suffer from this approach (anymore)
But you can't check wether they are of the same type, and you don't have a shared prototype that may affect all instances (maybe a pro or a con.)
My goto-approach If I need inheritance, is kind of a mix of both:
(if I just need a data-object I usually use a factory and plain objects).
If I add the following line to the top of the function I can also use that as a method to cast anything into an Instance of
Book
without cloning already existing instancesIn my opinion, I have the benefits of both worlds with this approach.