javascript 继承类的几种方法
1,对象冒充方法//可以实现多重继承
function Base()
{
this.id="1";
this.name="测试";
}
Base.prototype.className=function{
alert('Base');
};
function BaseA()
{
this.newMethod=Base;
this.newMethod();
delete this.newMethod;
this.type="class";
}
BaseA.prototype.Show=function{
alert('测试');
}
var A=new BaseA();
alert(A.type);
alert(A.name);
var B=new Base();
B.className();
2.call()方法
function Base(name)
{
this.id="1";
this.name=name;
}
Base.prototype.className=function{alert('base');};
function BaseA(name,type)
{
var obj=new Object()
Base.call(obj,name);
this.type=type;
}
BaseA.prototype.show=function{alert('测试');};
var A=new BaseA();
var B=new Base();
alert(B.name);
B.show();
alert(A.name);
A.className();
3.apply()方法
apply() 方法与call()方法基本一样,只是在最后的传值上有些区别,Call()方法传的是字符串,而apply()方法传的是数组.
function Base(name)
{
this.id="1";
this.name=name;
}
Base.prototype.className=function{alert('base');};
function BaseA(name,type)
{
var obj=new Object()
Base.apply(obj,new Array(name,));//关键在Array数组上与call()方法不同。
this.type=type;
}
BaseA.prototype.show=function{alert('测试');};
var A=new BaseA();
var B=new Base();
alert(B.name);
B.show();
alert(A.name);
A.className();
1,对象冒充方法//可以实现多重继承
function Base()
{
this.id="1";
this.name="测试";
}
Base.prototype.className=function{
alert('Base');
};
function BaseA()
{
this.newMethod=Base;
this.newMethod();
delete this.newMethod;
this.type="class";
}
BaseA.prototype.Show=function{
alert('测试');
}
var A=new BaseA();
alert(A.type);
alert(A.name);
var B=new Base();
B.className();
2.call()方法
function Base(name)
{
this.id="1";
this.name=name;
}
Base.prototype.className=function{alert('base');};
function BaseA(name,type)
{
var obj=new Object()
Base.call(obj,name);
this.type=type;
}
BaseA.prototype.show=function{alert('测试');};
var A=new BaseA();
var B=new Base();
alert(B.name);
B.show();
alert(A.name);
A.className();
3.apply()方法
apply() 方法与call()方法基本一样,只是在最后的传值上有些区别,Call()方法传的是字符串,而apply()方法传的是数组.
function Base(name)
{
this.id="1";
this.name=name;
}
Base.prototype.className=function{alert('base');};
function BaseA(name,type)
{
var obj=new Object()
Base.apply(obj,new Array(name,));//关键在Array数组上与call()方法不同。
this.type=type;
}
BaseA.prototype.show=function{alert('测试');};
var A=new BaseA();
var B=new Base();
alert(B.name);
B.show();
alert(A.name);
A.className();