I'm curious what the difference is between the following OOP javascript techniques. They seem to end up doing the same thing but is one considered better than the other?
function Book(title) { this.title = title; } Book.prototype.getTitle = function () { return this.title; }; var myBook = new Book('War and Peace'); alert(myBook.getTitle())
vs
function Book(title) { var book = { title: title }; book.getTitle = function () { return this.title; }; return book; } var myBook = Book('War and Peace'); alert(myBook.getTitle())
The Prototype Closure coding style organizes the constructor and prototype methods inside a closure for easy identification of the prototype object. var AdditionUtility = (function(){ var Constructor = function(value){ this. value = value || 0; }; Constructor. prototype = { value: null, add: function(numToAdd) { this.
There is a clear reason why you should use prototypes when creating classes in JavaScript. They use less memory. When a method is defined using this. methodName a new copy is created every time a new object is instantiated.
Well not exactly. Prototypes are a special type of object and exist as a property on function-objects. When we try to access a key on a function-object, JavaScript will look at its prototype property to see if it's there. If not it will go up the prototype-chain to try to find it.
To answer your question simply, there is no real difference. Straight from the MDN web docs definition: JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance.
The second one doesn't really create an instance, it simply returns an object. That means you can't take advantage of operators like instanceof
. Eg. with the first case you can do if (myBook instanceof Book)
to check if the variable is a type of Book, while with the second example this would fail.
If you want to specify your object methods in the constructor, this is the proper way to do it:
function Book(title) { this.title = title; this.getTitle = function () { return this.title; }; } var myBook = new Book('War and Peace'); alert(myBook.getTitle())
While in this example the both behave the exact same way, there are differences. With closure-based implementation you can have private variables and methods (just don't expose them in the this
object). So you can do something such as:
function Book(title) { var title_; this.getTitle = function() { return title_; }; this.setTitle = function(title) { title_ = title; }; // should use the setter in case it does something else than just assign this.setTitle(title); }
Code outside of the Book function can not access the member variable directly, they have to use the accessors.
Other big difference is performance; Prototype based classing is usually much faster, due to some overhead included in using closures. You can read about the performance differences in this article: http://blogs.msdn.com/b/kristoffer/archive/2007/02/13/javascript-prototype-versus-closure-execution-speed.aspx
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