寄生器繼承

function inheritPrototype(subClass,superClass){
     var p = inheritObject(superClass.prototype)
     p.constructor = subClass
     subClass.prototype = p
}
function inheritObject(o){
    function F(){}
    F.prototype = o
    return new F()
}


function SuperClass(name){
    this.name = name
    this.color = ['red','blue']
}
SuperClass.prototype.getName = function(){
     console.log(this.name)
}
function SubClass(name,time){
     SuperClass.call(this,name)
     this.time = time
}
inheritPrototype(SubClass,SuperClass)   
SubClass.prototype.getTime = function(){
    console.log(this.time)
}
var instance1 = new SubClass('js book',2014)
var instance2 = new SubClass('css book',2013)

instance1.color.push('ddddddd')
console.log(instance1.color)
console.log(instance2.color)
instance1.getName()
instance1.getTime()            

相關文章
相關標籤/搜索