I am trying to find if its possible and disadvantages adding an object to a prototype, for example
myObject.prototype = {
init: function () {
},
runMe: function () {
},
removeItems: function () {
}
}
Is this actually legal, and what's the difference for doing each one separately i.e.
myObject.prototype.init = function () {}
myObject.prototype.runMe = function () {}
I tried looking for MDN documentation about adding as an object but couldn't find anything. Can anyone comment on recommended ways of doing this?
Either approach is fine, and you should choose whichever approach makes it easier to do whatever you're doing. If this is the only place that you modify the prototype, then the only meaningful difference is that the first approach wipes away the constructor
property that's on the prototype by default.
For that reason, if you do overwrite the whole prototype
, you should make sure to re-add its constructor:
myObject.prototype = {
init: function () {
},
runMe: function () {
},
removeItems: function () {
},
// right here
constructor: myObject
}