Somebody asked me this question and i was a little lost by it.
As this is a Q&A it makes sense to ask here.
Why would I add a function to an objects prototype rather than add it to the object?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Consider these two objects:
Object 1
Object 2
Everytime we do this:
myObject1.myMethod also gets created. Where as when we do this:
myObject2.myMethod does not have the overhead of needing to be created again because it was added to the prototype chain.
This wouldn’t matter for a few instances but in a game, for example, where there could be hundreds of instances this can be quite a performance hit.
This net.tutsplus article may explain it better than me http://net.tutsplus.com/tutorials/javascript-ajax/prototypes-in-javascript-what-you-need-to-know/