What is the difference between adding a method to the JavaScript constructor directly and adding a method using a prototype?

I am using JavaScript Resig JavaScript style definition class . The following is an example class.

var Person = Class.extend({
  init: function(isDancing){
    this.dancing = isDancing;
  },
  dance: function(){
    return this.dancing;
  }
});

An alternative way to define a dance method:

Person.prototype.dance = function(){
   return this.dancing;
};

I like to use the first method, but someone suggested to me that it is inefficient. What is the difference between the two methods?

+3
source share
1 answer

I just figured out the solution myself.

, . . , () . .

, .

0

All Articles