javascript---类的继承

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



//第一种方式,冒充对象的方式.(利用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.[color=red]第二种方式:call()函数方式[/color]

call()函数是Function对象的一个函数,具体用法如下

//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.


所以继承可以这样实现:

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.[color=red]第三种实现方式:apply()函数方式,apply()和call()是一样的,只不过参数传递不同而已,apply的参数为数组[/color]


//第三种实现方式: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



4.[color=red] 第4种方式:原型链实现继承[/color]


//原型链实现继承
function Parent(){
}
Parent.prototype.hello = "hello";
Parent.prototype.sayHello = function(){
alert(this.hello);
}

function Child (){
}
Child.prototype = new Parent();//关键

Child.prototype.world = "world";
Child.prototype.sayWorld = function(){
alert(this.world);
}

var child = new Child();
child.sayHello();
child.sayWorld();


5.[color=red]第五种方式:混合模式实现继承[/color]

//混合模式实现继承
function Parent(hello){
this.hello = hello;
}
Parent.prototype.sayHello = function(){
alert(this.hello);
}

function Child(hello,world){
this.world = world;
Parent.call(this,hello);//关键

}
Child.prototype = new Parent(); //关键
Child.prototype.sayWorld = function(){
alert(this.world);
}

var child = new Child("hello","world");
child.sayHello();
child.sayWorld();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值