O'Reilly logo
  • Karl Giesing thinks this is interesting:

Here, we call myObject.doCool()


Cover of You Don't Know JS: this & Object Prototypes


Except that this does away with the main purpose of the prototype chain: polymorphism.

The whole idea is that you can pass myObject to some method/function expecting an object duck-typed to anotherObject. That method/function would call cool(), NOT doCool() - by design.

If that's not the intent, don't even attempt polymorphism. Either pass the other object to the method, or compose the object.

First technique: myObject.doCool(other) { other.cool(): }

Second technique: myObject.doCool() { // Either set in ctor or as a public property this.another.cool(); }