Created
May 5, 2015 13:10
-
-
Save nathggns/1507ae4da5278423a67f to your computer and use it in GitHub Desktop.
ES6 Greeter vs ES5 Greeter
This file contains 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 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