JS 繼承實例分析
更新時間:2008年11月04日 12:21:19 作者:
主要有三種方法: 1. this.method=Parent; this.method=Parent's constructor 2. Parent.call(this,arg,arg,arg.....);3.Parent.apply(this,arg.arg...) //for Array 還是來點實際的吧...
復(fù)制代碼 代碼如下:
function P(name){
this.name=name;
this.p1=function(){
alert('Parent Constructor');
}
return this;
}
function C(name,id){
//this.method=P;
//this.method(name); //1st method
//P.call(this,name); //2nd method
P.apply(this,new Array(name));//3rd method
this.id=id;
this.dis=function(){
alert(this.name);
}
}
function dis(){
alert(this.name);
}
function t(){
var cc=new C('N','Id');
cc.dis();
cc.p1();
}
相關(guān)文章
JavaScript中使用構(gòu)造函數(shù)實現(xiàn)繼承的代碼
JavaScript中借用構(gòu)造函數(shù)實現(xiàn)繼承的代碼,需要的朋友可以參考下。2010-08-08JavaScript面向?qū)ο?極簡主義法minimalist approach)
荷蘭程序員 Gabor de Mooij 提出了一種比 Object.create ()更好的新方法,他稱這種方法為極簡主義法(minimalist approach)。這也是我推薦的方法2012-07-07