0

I am currently trying to understand the JS prototype chain mechanism.

I have tried the following:

TestObject = function(){
  this.sayHello();
  this.sayGoodbye();
};

TestObject.prototype = {
  sayHello:function(){
    alert("Hi thereee");
  }
};

TestObject.prototype.prototype = {
  sayGoodbye:function(){
    alert("See yaaaaa");
  }
};

When I add

<script>var test = new TestObject();</script>

to my index.php, the following happens:

First, a window pops up, saying "Hi thereee", which is what I'd expect to happen. But then, the console tells me that the application crashed because 'sayGoodbye' is not a member of the test object.

So, my questtion is: what is happening here and how can I make actual prototype chains that are longer than just one single step?

Thanks a lot to everyone in advance.

J. D.
  • 113
  • 13
  • `this.prototype.sayGoodbye();` – gurvinder372 Nov 27 '17 at 14:21
  • You can't extend a prototype chain by assigning a `.prototype` property to a prototype. A prototype's relationship to _its_ prototype is the same as a `TestObject` instance's relationship to its prototype. I.e. that relationship is created by using `new` or `Object.create()`. – JLRishe Nov 27 '17 at 14:22

0 Answers0