1. Simple Object Literal
myApp.notepad = {}; myApp.notepad.writeable = true; myApp.notepad.font = 'helvetica'; myApp.notepad.setFont = function(theFont) { myApp.notepad.font = theFont; }
2. Nested Object Literal
myApp.notepad = { writeable: true, font: 'helvetica', setFont: function(theFont) { this.font = theFont; } }
3. Constructor using Object Literal
myApp.Notepad = function(defaultFont) { var that = {}; that.writeable = true; that.font = defaultFont; that.setFont = function(theFont) { that.font = theFont; } return that; } myApp.notepad1 = myApp.Notepad('helvetica');
4. Simple Constructor for new
myApp.Notepad = function(defaultFont) { this.writeable = true; this.font = defaultFont; this.setFont = function(theFont) { this.font = theFont; } } myApp.notepad1 = new myApp.Notepad('helvetica');
5. Prototype with Constructor for new
myApp.Notepad = function(defaultFont) { this.font = defaultFont; } myApp.Notepad.prototype.writeable = true; myApp.Notepad.prototype.setFont = function(theFont) { this.font = theFont; } myApp.notepad1 = new myApp.Notepad('helvetica');
The first two examples are best suited for creation of one-time objects. The last three describe templates for creation of multiple objects with a shared design.
All are useful. None are wrong. If I only needed one notebook in my app, I’d lean towards using Nested Object Literal since it neatly encapsulates all properties within its defining closures. For multiple notebook “instances” I like Prototype with Constructor for new simply because I can lock generic properties into the prototype object, making the constructor cleaner and more efficient
Next time I’ll talk about how to employ object inheritance with each methodology.
This is really helpful. When first learning javascript it took me a really long time to finally narrow down and sort out these differences. I use #5 the most but like you said it really depends on what you are doing.
Thanks for clearing out this concepts. Very well explained!
glad to help!
I wouldn’t list #4 as a proper way to create an object as the setFont function is not shared between instances in the prototype. This means that if you create 1000 notepad objects, you will have 1000 separate setFont functions in memory. With the method in #5, you would only have one setFont function that is shared between all notepad objects.
#3 is close to the module pattern, however I wouldn’t call it a constructor. If it returns a new object and is like a constructor, then better to say so in the name, e.g. “createNotepad”.
You might also include Lasse Nielson’s clone (also known as Crockford’s beget) function.
—
Rob
Hi Rob – yeah as I implied on JSMentors this isn’t a great post – it certainly needs updating
I don’t understand how the number 3 is equivalent to other four patterns, it is missing important bits such as this and new keyword. Wonder how it is equal to simple constructor pattern or are the differences between the two.
It is asked here as well:
http://stackoverflow.com/questions/9372758/javascript-object-creation-pattern
Can you elaborate it little further ?