0

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? :)

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

My bad, I didn't notice that chat() was instantiated later in the code using ch = new chat(). So, finally I ended up added new method to ch itself. Thanks anyways!
You can also assign anotherMethod to the instance by doing ch.anotherMethod = ..., but then you'll only be extending the instance rather than the class itself.
You are right, but got insightful info about prototype today, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.