ES 6問答-class継承
classの継承:
前の内容
class Point {
constructor(x, y) {
this.x = x
this.y = y
}
toString() {
return `(${this.x},${this.y})`
}
/* */
static hello() {
console.log("hello world!")
}
}
// ,
// super 。 super , this
class ColorPoint extends Point {
constructor(x, y, color) {
super(x, y) // super , , this
this.color = color
}
toString() {
// super ( ), Point.prototype
console.log(Point.prototype.toString === super.toString) // true
return `color: ${this.color}, ${super.toString()}`
}
}
class ColorPoint extends Point{
}
Point.prototype.toString === super.toString // true
/*
super , , , Point.prototype === super
*/
ColorPoint.hello()
は何を出力しますか?どうして?ハローワールドが出力されます!ColorPoint
はPoint
のサブクラスであるため、そのすべてのメソッドが継承されます.hello()
はPoint
の静的方法であり、布団類ColorPoint
にも継承される.Object.getPrototypeOf(ColorPoint) === Point //true
は、Object.getPrototypeOf(childClass)
の方法で、あるクラスが別のクラス__proto__
属性は、何を指していますか?子クラスの__proto__
属性は親クラスcolorPoint.__proto__ === Point // true
//
__proto__
プロパティは何を指しますか?サブクラスのprototype属性の__proto__
属性は、親クラスのprototype属性colorPoint.prototype.__proto__ === Point.prototype // true
//
Boolean()
Boolean 。 ,
, Boolean ,Boolean(exp) !!(exp)