Created
March 7, 2018 23:09
-
-
Save mrgenixus/673e919911e4a3b8b78b2001d8c5abb7 to your computer and use it in GitHub Desktop.
es6 classes
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
function ParentWidget(properties) { | |
Object.assign(this, properties); | |
} | |
function farewell() { | |
console.log('Goodbye Cruel ' + this.name); | |
} | |
ParentWidget.prototype.farewell = farewell; | |
function Widget(properties) { | |
ParentWidget.call(this, properties); | |
} | |
Widget.prototype.prototype = ParentWidget.prototype | |
Object.assign(Widget.prototype, { | |
farewell: function farewell() { | |
ParentWidget.prototype.farewell(); | |
}, | |
greet: function greet() { | |
console.log("Hello " + this.name); | |
} | |
}); | |
// usage: | |
var widget = new Widget({ name: 'Ben' }); | |
widget.greet(); // Hello Ben | |
widget.farewell(); // Goodbye Cruel Ben | |
//es6 | |
class ParentWidget { | |
constructor(properties) { | |
Object.assign(this, properties); | |
} | |
farewell() { | |
console.log('Goodbye Cruel ' + this.name); | |
} | |
} | |
class Widget extends ParentWidget { | |
farewell() { | |
super(); | |
} | |
greet() { | |
console.log("Hello " + this.name); | |
} | |
} | |
widget.greet(); // Hello Ben | |
widget.farewell(); // Goodbye Cruel Ben | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment