Here are three options of adding methods I need through the prototype. I was wondering if there are any issues with any of these patterns. Does it make a difference using the prototype like this or are they all the same just written differently.
after each option run I can call egg().alertMe();
These are just simplistic ways to ask the question.
Thanks for the help.
// option 1
// Methods are added to the egg functions prototype
// individually and the newly created
// objects prototype points to this prototype.
var egg = function() {
return new egg.init();
};
egg.prototype.alertMe = function () {alert('alertMe');};
egg.prototype.logMe = function () {console.log('logMe');};
// constructor function
egg.init = function() {};
egg.init.prototype = egg.prototype;
// option 2
// Methods are added to the egg functions prototype
// in an object literal and the newly created
// objects prototype points to this prototype.
var egg = function() {
return new egg.init();
};
egg.prototype = {
// Alert me.
alertMe: function () {alert('alertMe');},
// Log me.
logMe: function () {console.log('logMe');}
};
// constructor function
egg.init = function() {};
egg.init.prototype = egg.prototype;
// option 3
// Just add the methods to the the newly created
// objects prototype.
var egg = function() {
return new egg.init();
};
// constructor function
egg.init = function() {};
egg.init.prototype = {
// Alert me.
alertMe: function () {alert('alertMe');},
// Log me.
logMe: function () {console.log('logMe');}
};