1

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');}
};
Hello-World
  • 9,277
  • 23
  • 88
  • 154
  • I'd prefer `Object.assign(egg.prototype, {...})` – elclanrs Aug 20 '15 at 18:54
  • @elclanrs `Object.assign` is awesome but be careful if you're trying to use _getters/setters_ as it will copy the result of the getter instead. Also, why is there no `Object.create` choice in the question? – Paul S. Aug 20 '15 at 18:59
  • Thanks for the reply, because this is the type of pattern used to write jquery library and I am trying to understand the prototype. Also after reading Object.assign() on MDN that copies but I just want to reference not copy. – Hello-World Aug 20 '15 at 19:05
  • The differences resolve around `prototype.constructor` and `egg.prototype == init.prototype`. Other than that, [the whole thing is an antipattern](http://stackoverflow.com/a/12143833/1048572) :-) – Bergi Aug 20 '15 at 19:15
  • thanks what do you means antipattern? – Hello-World Aug 20 '15 at 19:20
  • @Hello-World: That you shouldn't use it. Just go for a standard constructor + prototype pattern, and use `new egg` everywhere. Don't copy jQuery only because it's popular. – Bergi Aug 20 '15 at 19:53
  • What reason do you have for wanting to call `egg().alertMe();`? Why do you want to return a new egg with each call to `egg()`? In your use case, why is it preferable to do this rather than more standard patterns: (1) making egg a simple object (`egg = { alertMe: function() {...} }`, which you can then call with `egg.alertMe()`), or (2) using the standard prototype pattern (i.e. throwing the "init" rigmarole, and just calling `myEgg = new egg(); myEgg.alertMe()`)? – Sam Fen Aug 20 '15 at 19:59

0 Answers0