Skip to content

Instantly share code, notes, and snippets.

@panych
Last active October 23, 2016 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panych/60925ab12a0cfa19d2b1d2852e813ab3 to your computer and use it in GitHub Desktop.
Save panych/60925ab12a0cfa19d2b1d2852e813ab3 to your computer and use it in GitHub Desktop.
Copy method to class in ES2015
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