The difference is how the object is created. When you define functions on a prototype of an object, it defines ONCE for each subsequent instance of that object.
If you declare functions at the instance level, they become overridden every time you declare a function.
In fact, this affects the performance of http://jsperf.com/prototype-vs-instance-functions
It is generally recommended that you use a prototype for functions that will be reused across multiple instances of the constructor. For example, if you use the new operator to create constructor instances ..
var Likes = function (el) { this.el = $(el); return this; }; Likes.prototype.add = function (name) { this.el.find('.no-results').remove(); $('<li>', { text: name }).appendTo(this.el); }; var oneLike = new Likes(); var twoLike = new Likes(); var threeLike = new Likes();
Since add defined on the prototype of the object, it receives the definition only once, and not every time a Likes instance is created.
jcreamer898
source share