JSOOP super构造器(基类构造器)

81 阅读1分钟
  • 直接继承子类获取不到基类的构造属性? 
function Shape(color) {
  this.color = color
}
Shape.prototype.duplicate = function() {
  console.log('duplicate')
}
function Circle(radius) {
  this.radius = radius
}

Circle.prototype = Object.create(Shape.prototype)
Circle.prototype.constructor = Circle
Circle.prototype.draw = function() {
  console.log('draw')
}
const s = new Shape()
const c = new Circle(1)

  •  通过call()方法实现
function Shape(color) {
  this.color = color
}
Shape.prototype.duplicate = function() {
  console.log('duplicate')
}
function Circle(radius, color) {
  Shape.call(this, color)
  this.radius = radius
}

Circle.prototype = Object.create(Shape.prototype)
Circle.prototype.constructor = Circle
Circle.prototype.draw = function() {
  console.log('draw')
}
const s = new Shape()
const c = new Circle(1, 'red')