ES6学習仕上げ(2)

クラスシンタックスシュガー

クラス自体は、関数であります

クラスMathHandle(){

  ビルダー(x、y)は{

    this.x = X

    this.y = Y

  }

  追加(){

    リターンthis.x + this.y

  }

}

CONST、M =新MathHandle(1,2)

console.log(m.add(1,2))

 

JSコンストラクタの使用法:

関数MathHandle(x、y)は{

  this.x = X。

  this.y = Y。

}

MathHandle.prototype.add =関数(){

  リターンthis.x + this.y

}

CONST、M =新MathHandle(1,2)

console.log(m.add(1,2))

コンストラクタMathHandle

MathHandle.prototype.constructor === MathHandle

M .__ proto__ === MathHandle.prototype

 

クラスの継承

クラス動物{

   コンストラクタ(名前){

     this.name =名前

   }

   食べる(){

     console.log( `$ {this.name} eat`)

   }

}

クラス犬{動物を拡張します

  コンストラクタ(名前){

    スーパー(名前)

    this.name =名前

  }

 

  いう(){

    console.log( `$ {this.name} say`)

  }

}

 

constの犬=新しい犬(「ハスキー」)

dog.say()

dog.eat()

注:文言に従ってください、それはスーパーコンストラクタでクラスの裏に書き拡張

おすすめ

転載: www.cnblogs.com/menghan94/p/12155857.html