Search code examples
javascriptmethodsmemberprototyping

What is the difference between saying “HTMLElement.prototype.someFunc =” and “HTMLElement.someFunc =”?


I have, in many ocassions been able to register methods for all HTMLElements in both ways.
So I am curious, is there any difference?
Should I prefer one method over the other one?
What would be the right way to do it?


Solution

  • using prototype is making the function available to all objects in the prototype chain. Its basically extending the class. While other one is just adding a property to the current element. But as Felix suggest the right thing would be not to do this at all.