Vikas Singhal
Vikas Singhal

Reputation: 836

Inheriting a Javascript Function

I have a JS definition like this in a .js file which is included in my .html file.

function chat() {
  this.sendNew() = function() { 
  [ .. ]
  }
}

Now I want to add another .js file which can extend this function with more methods, like this

function chat() { 
  this.anotherMethod = function() { 
  }
}

Is this possible? If yes, how? :)

Upvotes: 0

Views: 48

Answers (1)

Danilo Valente
Danilo Valente

Reputation: 11342

You can add methods/attributes to the .prototype object:

chat.prototype.anotherMethod = function () {
    // ...
}

Just remember that .prototype is a property of functions, and then should be accessed by chat, and not by one of its instances.

You can read more about .prototype here.

Upvotes: 4

Related Questions