Last active
October 23, 2016 12:58
-
-
Save panych/60925ab12a0cfa19d2b1d2852e813ab3 to your computer and use it in GitHub Desktop.
Copy method to class in ES2015
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 A { | |
constructor(msg) { | |
this.message = msg ? msg : 'default A message' | |
} | |
sayHello(additional) { | |
console.log(`${this.message}${additional ? ` and ${additional}` : ''}`) | |
} | |
} | |
class B { | |
constructor() { | |
this.message = 'default B message' | |
} | |
sayHello() { | |
A.prototype.sayHello.apply(this, arguments) | |
} | |
} | |
/* | |
Usage: | |
var a = new A; | |
var b = new B; | |
a.sayHello(); // default A message | |
b.sayHello(); // default B message | |
a.sayHello('more!'); // default A message and more! | |
b.sayHello('more more more!'); // default B message and more more more! | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment