js系列-4 继承
2016-02-26 14:55
591 查看
继承能达到代码的复用,js的继承不是基于类型的继承,而是基于原型的继承,也就是说直接从其他已有对象继承。
当一个函数对象被创建时,Function构造器产生的函数对象会运行类似这样的一些代码:
this.prototype={constructor:this}
这个prototype对象是存放继承特征的地方,因为这个对象在出现在实例的原型链上。
js不知道哪个函数对象会作为构造器,所以每个函数都有prototype属性。
1),对象冒充:
//对象冒充方式实现继承
functionParent(name){
this.name = name;
this.showName = function(){
alert(this.name);
}
}
functionChild(name,age){
//最重要的三行代码,先设一个引用指向一个函数,
//这个函数是父对象的构造方法,然后用引用调用这个函数
//这时候父对象的this已经变成子对象了,这样就将子对象的name赋值
//并且子对象也拥有了showName这个方法
//然后删除该引用
this.method =Parent;
this.method(name);
deletethis.method;
this.age=age;
this.showAge =function(){
alert(age);
}
}
varp = new Parent("zhang");
p.showName();
varc = new Child("li",12);
c.showName();
c.showAge();
2)c all方式实现继承:
c all方法的使用,c all方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法,可以通过函数名来调用c all方法,c all方法的第一个参数会被传递给函数中的this。从第二个参数开始,逐一赋值给函数中的参数。
function Hello(age,gender){
alert(this.name+" : "+age+" :"+gender);
}
var obj = new Object();
obj.name="zhang"
Hello.c all(obj,12,"M");
打印出来的是:zhang : 12 : M
//使用c all方法实现继承
function Parent(name){
this.name = name;
this.sayHello=function(){
alert(this.name);
}
}
function Child(name,password){
Parent.c all(this,name);
this.password= password;
this.sayWorld=function(){
alert(this.password);
}
}
var parent = newParent("zhang");
var child = newChild("lisi","123");
parent.sayHello();
child.sayHello();
child.sayWorld();
3)使用a pply方法实现继承:
a pply也是Function对象的一个方法,他的使用方法与c all一样,只是第二个参数以后的参数是以数组形式呈现的。
//使用appl y方法实现继承
function Parent(name){
this.name = name;
this.sayHello=function(){
alert(this.name);
}
}
function Child(name,password){
Parent.app ly(this,[name]);
this.password= password;
this.sayWorld=function(){
alert(this.password);
}
}
var parent = newParent("zhang");
var child = newChild("lisi","123");
parent.sayHello();
child.sayHello();
child.sayWorld();
4)使用原型链的方式实现继承:
//使用原型链方式实现继承
functionParent(){}
Parent.prototype.name="Hello";
Parent.prototype.sayHello=function(){
alert(this.name)
};
function Child(){}
Child.prototype=new Parent();
Child.prototype.password="world";
Child.prototype.sayWorld=function(){
alert(this.password)
};
var child = newChild();
child.sayHello();
child.sayWorld();
这种方式的缺点是无法实现参数传递
5)使用混合方式(原型+c all)实现继承(推荐)
//使用混合方式实现继承
function Parent(name){
this.name=name;
}
Parent.prototype.sayHello=function(){
alert(this.name)
};
function Child(name,password){
Parent.c all(this,name);
this.password=password;
}
Child.prototype=newParent();
//上面等于Child.prototype.__proto__ = Parent.prototype
Child.prototype.sayWorld=function(){
alert(this.password)
};
var child = newChild("zhang","123");
child.sayHello();
//1,child上找(找不到)2,child.__proto__(Child.prototype)上找(找不到)。3,Child.prototype.__proto__(Parent.prototype)上找(找到了)
child.sayWorld();
当一个函数对象被创建时,Function构造器产生的函数对象会运行类似这样的一些代码:
this.prototype={constructor:this}
这个prototype对象是存放继承特征的地方,因为这个对象在出现在实例的原型链上。
js不知道哪个函数对象会作为构造器,所以每个函数都有prototype属性。
1),对象冒充:
//对象冒充方式实现继承
functionParent(name){
this.name = name;
this.showName = function(){
alert(this.name);
}
}
functionChild(name,age){
//最重要的三行代码,先设一个引用指向一个函数,
//这个函数是父对象的构造方法,然后用引用调用这个函数
//这时候父对象的this已经变成子对象了,这样就将子对象的name赋值
//并且子对象也拥有了showName这个方法
//然后删除该引用
this.method =Parent;
this.method(name);
deletethis.method;
this.age=age;
this.showAge =function(){
alert(age);
}
}
varp = new Parent("zhang");
p.showName();
varc = new Child("li",12);
c.showName();
c.showAge();
2)c all方式实现继承:
c all方法的使用,c all方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法,可以通过函数名来调用c all方法,c all方法的第一个参数会被传递给函数中的this。从第二个参数开始,逐一赋值给函数中的参数。
function Hello(age,gender){
alert(this.name+" : "+age+" :"+gender);
}
var obj = new Object();
obj.name="zhang"
Hello.c all(obj,12,"M");
打印出来的是:zhang : 12 : M
//使用c all方法实现继承
function Parent(name){
this.name = name;
this.sayHello=function(){
alert(this.name);
}
}
function Child(name,password){
Parent.c all(this,name);
this.password= password;
this.sayWorld=function(){
alert(this.password);
}
}
var parent = newParent("zhang");
var child = newChild("lisi","123");
parent.sayHello();
child.sayHello();
child.sayWorld();
3)使用a pply方法实现继承:
a pply也是Function对象的一个方法,他的使用方法与c all一样,只是第二个参数以后的参数是以数组形式呈现的。
//使用appl y方法实现继承
function Parent(name){
this.name = name;
this.sayHello=function(){
alert(this.name);
}
}
function Child(name,password){
Parent.app ly(this,[name]);
this.password= password;
this.sayWorld=function(){
alert(this.password);
}
}
var parent = newParent("zhang");
var child = newChild("lisi","123");
parent.sayHello();
child.sayHello();
child.sayWorld();
4)使用原型链的方式实现继承:
//使用原型链方式实现继承
functionParent(){}
Parent.prototype.name="Hello";
Parent.prototype.sayHello=function(){
alert(this.name)
};
function Child(){}
Child.prototype=new Parent();
Child.prototype.password="world";
Child.prototype.sayWorld=function(){
alert(this.password)
};
var child = newChild();
child.sayHello();
child.sayWorld();
这种方式的缺点是无法实现参数传递
5)使用混合方式(原型+c all)实现继承(推荐)
//使用混合方式实现继承
function Parent(name){
this.name=name;
}
Parent.prototype.sayHello=function(){
alert(this.name)
};
function Child(name,password){
Parent.c all(this,name);
this.password=password;
}
Child.prototype=newParent();
//上面等于Child.prototype.__proto__ = Parent.prototype
Child.prototype.sayWorld=function(){
alert(this.password)
};
var child = newChild("zhang","123");
child.sayHello();
//1,child上找(找不到)2,child.__proto__(Child.prototype)上找(找不到)。3,Child.prototype.__proto__(Parent.prototype)上找(找到了)
child.sayWorld();
相关文章推荐
- Extjs4.0 最新最全视频教程
- Javascript中toFixed方法的改进
- 5个常见可用性错误和解决方案
- js可突破windows弹退效果代码
- JSP脚本漏洞面面观
- 使用BAT一句话命令实现快速合并JS、CSS
- js显示当前星期的起止日期的脚本
- 爆炸式的JS圆形浮动菜单特效代码
- js select常用操作控制代码
- JS实现不使用图片仿Windows右键菜单效果代码
- 从jsp发送动态图像
- 原生js结合html5制作小飞龙的简易跳球
- js 页面模块自由拖动实例
- js实现小鱼吐泡泡在页面游动特效
- js 提交和设置表单的值
- PHP VBS JS 函数 对照表
- node.js抓取并分析网页内容有无特殊内容的js文件
- PHP+JS实现大规模数据提交的方法
- 仿51JOB的地区选择效果(可选择多个地区)
- js身份证验证超强脚本