I'm creating a plugin for JavaScript, and I want to add usable methods to it. (ex. myPlugin.usefulMethod()
) The way I would do it is as follows:
function MyPlugin(param1, param2) {
...
this.usefullMethod = function() {...};
this.anotherUserfullMethod = function(someParam) {...};
}
var plugin1 = new MyPlugin(someParam1, someParam2);
plugin1.usefullMethod();
But I've see a lot of plugin create their methods like this:
function MyPlugin(param1, param2) {
...
}
MyPlugin.prototype.usefullMethod = function() {...};
MyPlugin.prototype.anotherUserfullMethod = function(someParam) {...};
Which is the correct way, and why? What's the difference between them?