-
そして、クラスのコンストラクタとして
-
プロパティとメソッドが書かれて分離します
// 老版本 関数ユーザー(名前、パス){ この .nameの= 名前 この .pass = 通過 } User.prototype.showName = 関数(){ にconsole.log(この.nameの) } User.prototype.showPass = 関数( ){ にconsole.log(この.pass) } VARの U1 = 新しいユーザー( 'でき'、 '1233' ) u1.showName() u1.showPass() // 老版本继承 関数VipUserは(名前は、合格レベル){ User.call(この、名前は、)を通過 この .level = レベル } VipUser.prototype = 新しいユーザー() VipUser.prototype.constructor = VipUser VipUser.prototype.showLevel = 関数(){ にconsole.log(この.level) } VARの V1 = 新しい VipUserを( '1234'、 '青'、3 ) v1.showName() v1.showLevel()
-
classキーワードでは、コンストラクタ
-
直接添加クラスメソッド
-
継承、スーパースーパークラス==親
クラスユーザー{ コンストラクタ(名前、パス){ この .nameの= 名前 この .pass = パス } showName(){ にconsole.log(この.nameの) } showPass(){ にconsole.log(この.pass) } } VARの U1 = 新しいユーザー( 'able2'、 '111' ) u1.showName(); // able2
u1.showPass(); // 111
// 新版本继承
class VipUser extends User {
constructor(name, pass, level) {
super(name, pass)
this.level = level
}
showLevel(){
console.log(this.level)
}
}
v1 = new VipUser('blue', '123', 3)
v1.showLevel();// 3