首页 > 代码库 > js 中组合继承

js 中组合继承

function subType(){
  this.addColor = [1,2,3,4,5,6];
}
subType.prototype.showMess = function(){
  console.log(this.addColor)
}
function type(){
   subType.call(this)
}
type.prototype = new subType

subType {addColor: Array[6]}
var t = new type
undefined
t.addColor
[1, 2, 3, 4, 5, 6]
t.showMess()
VM10694:5 [1, 2, 3, 4, 5, 6]
undefined
t.addColor.push(333)
7
t.showMess()
VM10694:5 [1, 2, 3, 4, 5, 6, 333]
undefined
var t2 = new type
undefined
t2.addColor
[1, 2, 3, 4, 5, 6]
t2.showMess()
VM10694:5 [1, 2, 3, 4, 5, 6]

  

js 中组合继承