Typescript Deep Dive by Basarat Ali Syed
Typescript Deep Dive by Basarat Ali Syed
Typescript Deep Dive by Basarat Ali Syed
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
TypeScript <strong>Deep</strong> <strong>Dive</strong><br />
super<br />
Note that if you call super on a child class it is redirected to the prototype as shown below:<br />
class Base {<br />
log() { console.log('hello world'); }<br />
}<br />
class Child extends Base {<br />
log() { super.log() };<br />
}<br />
generates:<br />
ar Base = (function () {<br />
function Base() {<br />
}<br />
Base.prototype.log = function () { console.log('hello world'); };<br />
return Base;<br />
})();<br />
var Child = (function (_super) {<br />
__extends(Child, _super);<br />
function Child() {<br />
_super.apply(this, arguments);<br />
}<br />
Child.prototype.log = function () { _super.prototype.log.call(this); };<br />
return Child;<br />
})(Base);<br />
Notice _super.prototype.log.call(this) .<br />
This means that you cannot use super on member properties. Instead you should just use this .<br />
class Base {<br />
log = () => { console.log('hello world'); }<br />
}<br />
class Child extends Base {<br />
logWorld() { this.log() };<br />
}<br />
Notice since there is only one this shared between the Base and the Child class you need to use different names (here<br />
log and logWorld ).<br />
Also Note that TypeScript will warn you if you try to misuse super :<br />
module quz {<br />
class Base {<br />
log = () => { console.log('hello world'); }<br />
}<br />
}<br />
class Child extends Base {<br />
// ERROR : only `public` and `protected` methods of base class are accessible via `super`<br />
logWorld() { super.log() };<br />
}<br />
Fork me on github<br />
Classes Super<br />
18