1.利用對象的動態特性添加成員
varo={};
o.name=‘jim’
varPerson=function(){};
Person.prototype.sayHello=function(){
alert(“xxxx")
}
//此時原型對象是對象,可以利用動態特性隨時添加成員
//添加的成員都會被構造函數的對象所繼承
2.利用覆蓋原型對象
varPerson=function(){};
Person.prototype={
say:function(){},
said:function(){}
constructor:Person;//添加constructor
}
如果使用這種方法會覆蓋掉原有的prototype方法(包括constructor) ?指向為構造函數的constructor
一定要給新對象添加一個constructor屬性
3.利用組合式繼承添加屬性
利用extend方法(在我之前的博客中有)
extend()
varPerson=function(){};
Person.prototype={
say:function(){},
said:function(){}
constructor:Person;//添加constructor
}
Person.extend=function(?msg?){
for(varkinmsg){
Person.prototype[k]=msg[k]
}
}
原文參考入口三種方法