JSOOP 构造函数动态创建对象

89 阅读1分钟
function Shape() {

}
Shape.prototype.duplicate = function() {
  console.log('duplicate')
}
function Circle(radius) {
  this.radius = radius
}

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

 

function Shape() {

}
Shape.prototype.duplicate = function() {
  console.log('duplicate')
}
function Circle(radius) {
  this.radius = radius
}

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

 重设原型的时候最好也重设构造器函数

function Shape() {

}
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)