Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@moshest
Created September 10, 2016 21:43
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 moshest/c4ee18366b233128fe6fc7f3ebb43dff to your computer and use it in GitHub Desktop.
Save moshest/c4ee18366b233128fe6fc7f3ebb43dff to your computer and use it in GitHub Desktop.
ES6 methods implementation
class Foo {
test() {
return 'Foo';
}
}
class Bar extends Foo {
test() {
return `${super.test()}Bar`;
}
}
class FooX {
test() {
return 'FooX';
}
}
class BarX extends FooX {
test() {
return `${super.test()}Bar`;
}
}
const bar = new Bar();
// The same signature but different behavior
console.log(Bar.prototype.test.toString() === BarX.prototype.test.toString()); // true
console.log(Bar.prototype.test.call(bar)); // "FooBar"
console.log(BarX.prototype.test.call(bar)); // "FooXBar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment