Skip to content

Instantly share code, notes, and snippets.

@nathggns
Created May 5, 2015 13:10
Show Gist options
  • Save nathggns/1507ae4da5278423a67f to your computer and use it in GitHub Desktop.
Save nathggns/1507ae4da5278423a67f to your computer and use it in GitHub Desktop.
ES6 Greeter vs ES5 Greeter
class Greeter {
name;
constructor(name) {
this.name = name;
}
greet() {
return `Hello, ${this.name}`;
}
}
class BobGreeter extends Greeter {
constructor() {
super('Bob');
}
}
var bobGreeter = new BobGreeter();
console.log(bobGreeter.greet());
// Hello, Bob
// vs
function FGreeter(name) {
this.name = name;
}
FGreeter.prototype.greet = function() {
return 'Hello, ' + this.name;
}
function FBobGreeter() {
FGreeter.call(this, 'Bob');
}
FBobGreeter.prototype = FGreeter.prototype;
FBobGreeter.prototype.constructor = FBobGreeter;
var fBobGreeter = new FBobGreeter();
console.log(fBobGreeter.greet());
// Hello, Bob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment