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
//今までコンストラクタ関数とprototypeを使用して表現してきた | |
//クラス構造を、クラス用の構文で定義できる。 | |
//ES5 | |
var Person = function(name) { | |
this.name = name; | |
}; | |
Person.prototype.hi = function() { | |
return 'hi, my name is ${this.name}'; | |
}; | |
var bob = new Person('Bob'); | |
console.log(bob.hi()); //Hi, my name is Bob | |
//ES6 | |
class Person { | |
constructor(name) { | |
this.name = name; | |
} | |
hi() { | |
return 'Hi, my name is ${this.name}'; | |
} | |
} | |
var tom = new Person('Tom'); | |
console.log(tom.hi()); //Hi, my name is Tom | |
console.log(typeof Person); //function | |
//継承も可能 | |
class Noisy extends Person { | |
hi() { | |
return '${super.hi()}!!!!!!!!!'; | |
} | |
} | |
var mike = new Noisy('Mike'); | |
console.log(mike.hi()); //Hi, my name is Mike!!!!!!!!!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment