Last active
August 29, 2015 14:14
-
-
Save A/8cd490d89ce6c9b18800 to your computer and use it in GitHub Desktop.
Inheritance problems on ES2015 with properties before 'super'.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class View { | |
constructor() { | |
// require tagName and className to create element | |
} | |
} | |
class Input extends View { | |
constructor(...args) { | |
this.tagName = 'input'; | |
this.className = 'form-control input'; | |
super(...args); | |
} | |
} | |
class TextArea extends Input { | |
constructor(...args) { | |
this.tagName = 'textarea'; | |
this.className = 'form-control textarea'; | |
super(...args); | |
} | |
} | |
console.log('create new Input'); | |
var input = new Input(); | |
console.assert(input.tagName === 'input'); //ok | |
console.assert(input.className = 'form-control input'); // ok | |
console.log('create new TextArea'); | |
var textarea = new TextArea(); | |
console.assert(textarea.tagName === 'textarea'); // fail | |
console.assert(textarea.className === 'form-control textarea'); // fail | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simplest example | |
class A { | |
x = 5; | |
} | |
class B extends A { | |
x = 0; | |
} | |
let b = new B(); | |
console.log(b.x); // 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Link to traceur playground with this code