javascript 类继承

王朝学院·作者佚名  2009-02-17
窄屏简体版  字體: |||超大  

1.第一种方式,冒充对象的方式.(利用js里的每一个方法名都是一个Function对象) Java代码

function Parent(username){

this.username = username;

this.say = function(){

alert(this.username);

}

}

function Child(username,password){

this.temp = Parent;//temp指向Parent所指向的地方 。 利用js里的每一个方法名都是一个Function对象,指向一个方法。

this.temp(username);//初始化方法里的内容

delete this.temp;//temp没有用了。可以直接删除掉.this不可以丢了

//Parent(username);//这样写表面看起来是正确的,其实是错误的。因为只有new出来的对象才有this,所以调用Parent里的this就没有值了

this.password = password;

this.hello = function(){

alert(this.password);

}

}

var parent = new Parent("zhangsan");

parent.say();//zhangsan

var child = new Child("lisi","123456");

child.say();//lisi

child.hello();//123456

//第一种方式,冒充对象的方式.(利用js里的每一个方法名都是一个Function对象)

function Parent(username){

this.username = username;

this.say = function(){

alert(this.username);

}

}

function Child(username,password){

this.temp = Parent;//temp指向Parent所指向的地方 。 利用js里的每一个方法名都是一个Function对象,指向一个方法。

this.temp(username);//初始化方法里的内容

delete this.temp;//temp没有用了。可以直接删除掉.this不可以丢了

//Parent(username);//这样写表面看起来是正确的,其实是错误的。因为只有new出来的对象才有this,所以调用Parent里的this就没有值了

this.password = password;

this.hello = function(){

alert(this.password);

}

}

var parent = new Parent("zhangsan");

parent.say();//zhangsan

var child = new Child("lisi","123456");

child.say();//lisi

child.hello();//123456

2.第二种方式:call()函数方式 call()函数是Function对象的一个函数

具体用法如下 Java代码

//call()函数是Function对象的一个函数

//具体用法如

function test(str){

alert(this.username + "," + str);

}

var o = new Object();

o.username = "zhangsan";

test.call(o,"123456");//zhangsan,123456 .因为每个Function 对象都有一个call()方法,而函数名就是一个Function对象.call()函数的第一个参数是test函数里的this.

//call()函数是Function对象的一个函数

//具体用法如

function test(str){

alert(this.username + "," + str);

}

var o = new Object();

o.username = "zhangsan";

test.call(o,"123456");//zhangsan,123456 .因为每个Function 对象都有一个call()方法,而函数名就是一个Function对象.call()函数的第一个参数是test函数里的this.

Java代码

function Parent(username){

this.username = username;

this.say = function(){

alert(this.username);

}

}

function Child(username,password){

Parent.call(this,username);

this.password = password;

this.hello = function(){

alert(this.password);

}

}

var parent = new Parent("zhangsan");

parent.say();//zhangsan

var child = new Child("lisi","123456");

child.say();//lisi

child.hello();//123456

function Parent(username){

this.username = username;

this.say = function(){

alert(this.username);

}

}

function Child(username,password){

Parent.call(this,username);

this.password = password;

this.hello = function(){

alert(this.password);

}

}

var parent = new Parent("zhangsan");

parent.say();//zhangsan

var child = new Child("lisi","123456");

child.say();//lisi

child.hello();//123456

3.第三种实现方式:apply()函数方式

apply()和call()是一样的,只不过参数传递不同而已,apply的参数为数组 Java代码

//第三种实现方式:apply()函数方式,apply()和call()是一样的,只不过参数传递不同而已,apply的参数为数组

//所以继承可以这样实现

function Parent(username){

this.username = username;

this.say = function(){

alert(this.username);

}

}

function Child(username,password){

Parent.apply(this,new Array(username));

this.password = password;

this.hello = function(){

alert(this.password);

}

}

var parent = new Parent("zhangsan");

parent.say();//zhangsan

var child = new Child("lisi","123456");

child.say();//lisi

child.hello();//123456

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航