There are no methods in JavaScript. TypeScript tries to hide the work of the prototype with the familiar notation, but, like all abstractions, it is imperfect. The essence of the prototype chain is that when c.DoStuff() is called, it searches for DoStuff in instance c than prototype (C), then its prototype (B), then its prototype (A), and then its prototype (Object).
This applies not only to functions, but to any member that you can find. Functions are not very specific in JavaScript, which, ironically, makes them powerful.
Using the TypeScript syntax in your class A, you put the DoStuff function in prototype A. Each instance of A that you call DoStuff on will look for it in the instance (and probably won't find it there), look at the prototype and see which function you have identified. So far so good.
Then you define a class B that extends A, so the prototype chain is B-> A-> Object. Inside Constructor B, you assign a new copy of the DoStuff function to each individual instance. (This uses a lot more memory.) Therefore, when you build a new B and call DoStuff on it, there is a function that does not parse the prototype. So that works.
Now you define a class C extending B, and the prototype chain C-> B-> A-> Object. Again, each C instance receives a copy of the function assigned by DoStuff. But inside this constructor, we do not grab the DoStuff from instance B, we grab it from the prototype, and we did not add this function directly to the prototype. Not finding it there, we will go up the prototype chain to A.prototype and find the DoStuff member to use from there. Therefore, C has a link to A DoStuff.
You can get your code to work as expected if you did something like this:
class B extends A { constructor() { super(); } } B.prototype.DoStuff = function () { return A.prototype.DoStuff() + " and Called from B"; }
But that is just plain stupid.
Jeffery grajkowski
source share