I am using John Resig’s JavaScript class definition style. Below is an example class.
var Person = Class.extend({
init: function(isDancing){
this.dancing = isDancing;
},
dance: function(){
return this.dancing;
}
});
An alternative way to define the dance method would be:
Person.prototype.dance = function(){
return this.dancing;
};
I like using the first way but someone suggested me that it is inefficient. What is the difference between the two ways?
Just found out the solution myself.
John Resig’s extend function automatically creates a constructor from the object passed as an argument. In the first way, the dance method in the object would automatically be assigned to the prototype of the returned object. It means the returned constructor (class) would be in fact using the 2nd style. So it is unnecessary to use the 2nd way.
Thus when using John Resig’s code the first way is NOT inefficient.