DOM笔记(十三):JavaScript的继承方式
在Java、C++、C#等OO语言中,都支持两种继承方式:接口继承和实现继承。接口继承制继承方法签名,实现继承则继承实际的方法和属性。在SCMAScript中,由于函数没有签名,所以无法实现接口继承,只支持实现继承。
实现继承主要依靠原型链来实现。
一、原型链
原型链是利用原型让一个引用类型继承另一个引用类型的方法,在DOM笔记(十二):又谈原型对象中,描述了构造函数、实例和原型之间的关系:
每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而每个实例都包含一个指向原型对象的内部指针。如果将原型对象作为另一个构造函数的实例,会如何?
function SuperType()
{
this.property = true;
}
SuperType.prototype.getSuperValue=function()
{
return this.property;
};
function SubType()
{
this.subproperty = false;
}
// 继承SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue=function()
{
return this.subproperty;
};
var instance = new SubType();
alert(instance.getSuperValue()); //true
在DOM笔记(十一):JavaScript对象的基本认识和创建中提到了,Object类型是所有对象的根元素。所以,SubType也是继承了Object,内部有一个指针指向Object的原型。 现在,他们的关系变成了这样:
当instance调用getSuperValue()时,会进行搜索:1)在实例中搜索;2)在实例中找不到,就在SubType.prototype中搜索;3)在SubType.prototype中找不到,就在SuperType.prototype中找。找到后就停止搜索,返回结果值。但instance调用toString()时,就一直会搜索到原型链的末端—Object.prototype。
根据原型链,需要注意的是,instance.constructor不再指向SubType,而是指向了SuperType。
二、原型继承小结
1、确定原型跟实例的关系
在DOM笔记(十二):又谈原型对象中提到了isPrototypeOf()方法,另外也可以用instanceof确立这种关系的存在。
//均返回true
alert(instance instanceof Object);
alert(instance instanceof SuperType);
alert(instance instanceof SubType);
alert(Object.prototype.isPrototypeOf(instance));
alert(SuperType.prototype.isPrototypeOf(instance));
alert(SubType.prototype.isPrototypeOf(instance));
2、当在子类中定义与父类中同名的属性和方法时,父类中对应的属性和方法会被屏蔽。
function SuperType()
{
this.property = true;
}
SuperType.prototype.getSuperValue=function()
{
return this.property;
};
function SubType()
{
this.property = false; //同名属性
}
// 继承SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue=function()
{
return this.property;
};
var instance = new SubType();
alert(instance.getSuperValue()); //false
function SuperType()
{
this.property = true;
}
SuperType.prototype.getSuperValue=function()
{
return this.property;
};
function SubType()
{
this.subproperty = false;
}
// 继承SuperType
SubType.prototype = new SuperType();
// 使用字面量添加新方法,导致上一行无效
SubType.prototype={
getSubValue:function()
{
return this.subproperty;
}
};
var instance = new SubType();
alert(instance.getSuperValue()); //error:undefined is not a function
4、原型继承存在两个问题:1)引用类型的原型属性会被所有实例共享;2)原型继承时,不能通过子类的实例向父类的构造函数中传递参数。
function SuperType()
{
this.colors = ["red","blue","white"];
}
function SubType(){ }
// 继承SuperType
SubType.prototype = new SuperType();
var instance1 = new SubType();
instance1.colors.push("black");
alert(instance1.colors); //red,blue,white,black
var instance2 = new SubType();
alert(instance2.colors); //red,blue,white,black
instance1和instance2共享了colors数组属性(引用类型)。
三、其他继承方式
1、借用构造函数
利用原型继承时,引用类型的原型属性会被所有实例共享。但是借用构造函数就能避免这种问题,并且能够利用apply()和call()传递参数给父类的构造函数:
function SuperType()
{
this.colors = ["red","blue","white"];
}
function SubType()
{
// 继承SuperType
SuperType.call(this);
}
var instance1 = new SubType();
instance1.colors.push("black");
alert(instance1.colors); //red,blue,white,black
var instance2 = new SubType();
alert(instance2.colors); //red,blue,white
function SuperType(name)
{
this.name=name;
this.colors = ["red","blue","white"];
}
SuperType.prototype.sayName=function()
{
alert(this.name);
};
function SubType(name,age)
{
// 继承SuperType属性,并传递参数给父类的构造函数
SuperType.call(this,name); //第一次调用SuperType()
this.age=age;
} // 继承SuperType方法
SubType.prototype = new SuperType(); //第二次调用SuperType()
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge=function()
{
alert(this.age);
};
var instance1 = new SubType("dwqs",20);
instance1.colors.push("black");
alert(instance1.colors); //red,blue,white,black
instance1.sayName(); //dwqs
instance1.sayAge(); //20
var instance2 = new SubType("i94web",25);
alert(instance2.colors); //red,blue,white
instance2.sayName(); //i94web
instance2.sayAge(); //25
function object(o)
{
function F(){}
F.prototype=o;
return new F();
}
var person={
name:“dwqs”,
colors:["red","blue","white"]
};
var per1 = object(person);
alert(per1.name); //dwqs
// 覆盖已有的属性
per1.name = “i94web”;
per1.age = 20; //新增属性
alert(per1.age); //20
per1.colors.push(“black”);
alert(per1.name); //i94web
alert(per1.colors); //red,blue,white,black
var per2 = object(person);
alert(per2.name); //dwqs
// 所有实例共享引用类型的属性
alert(per2.colors); //red,blue,white,black
其混乱关系如下:
per1
和per2会共享colors属性(引用类型)。在ECMAScript
5中,Object.create(obj,[props])规范了这种方式:obj是已经存在的对象,props可选,是一个包含额外属性的对象,格式
与Object.defineProperties()的第二个参数格式一样。
var person={
name:"dwqs",
colors:["red","blue","white"]
};
var per1 = Object.create(person,{
age:{
value:20
}
});
alert(per1.age); //20
4、寄生式继承
该方式与原型式继承差不多,也要借用object函数,然后在内部已某种方式来增强对象。
function object(o)
{
function F(){}
F.prototype=o;
return new F();
}
var person={
name:"dwqs",
colors:["red","blue","white"]
};
function createObj(obj)
{
var clone = object(obj);
// 增强对象
clone.age = 20;
clone.Hello=function()
{
alert("Hello,my age is "+this.age);
};
return clone;
}
var per1 = createObj(person);
per1.Hello(); //Hello,my age is 20
5、寄生组合式继承
在第2中方式,组合继承虽然时JavaScript钟最常用的方式,但是也存在不足,上面已经提到,不再赘述。而寄生组合式继承能解决这两个不足,其基本模式如下:
function object(o)
{
function F(){}
F.prototype=o;
return new F();
}
function inheritPrototype(subType,superType)
{
var obj = object(superType.prototype); //创建对象
obj.constructor = subType; //增强对象
subType.prototype = obj; //指定对象
}
重写组合继承中的示例:
function object(o)
{
function F(){}
F.prototype=o;
return new F();
}
function inheritPrototype(subType,superType)
{
var obj = object(superType.prototype); //创建对象
obj.constructor = subType; //增强对象
subType.prototype = obj; //指定对象
}
function SuperType(name)
{
this.name=name;
this.colors = ["red","blue","white"];
}
SuperType.prototype.sayName=function()
{
alert(this.name);
};
function SubType(name,age)
{
// 继承SuperType属性,并传递参数给父类的构造函数
SuperType.call(this,name);
this.age=age;
}
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge=function()
{
alert(this.age);
};
var instance1 = new SubType("dwqs",20);
instance1.colors.push("black");
alert(instance1.colors); //red,blue,white,black
instance1.sayName(); //dwqs
instance1.sayAge(); //20
var instance2 = new SubType("i94web",25);
alert(instance2.colors); //red,blue,white
instance2.sayName(); //i94web
instance2.sayAge(); //25
只调用了一次SuperType()构造函数,并且避免了SuperType.prototype上创建多余和不必要的属性,原型链保持不变,能够使用instanceof和isPrototypeOf()方法。
原文首发:http://www.ido321.com/1381.html
DOM笔记(十三):JavaScript的继承方式的更多相关文章
- JavaScript各种继承方式和优缺点
好久没写博客啦,嘻嘻,这个月是2017年的最后一个月啦,大家应该都开始忙着写年终总结了吧,嘻嘻,小颖今天给大家分享下Javascript中的几种继承方式以及他们的优缺点. 1.借助构造函数实现继承 原 ...
- javascript中继承方式及优缺点(一)
分别介绍原型链继承.call/apply继承(借用构造函数继承).组合继承.原型式继承.寄生式继承.寄生组合式继承 1. 原型链继承 核心:将父类的实例作为子类的原型 function SuperTy ...
- javascript中继承方式及优缺点(三)
文以<JavaScript高级程序设计>上的内容为骨架,补充了ES6 Class的相关内容,从我认为更容易理解的角度将继承这件事叙述出来,希望大家能有所收获. 1. 继承分类 先来个整体印 ...
- javascript中继承方式及优缺点(二)
一.原型链继承 方式1: 原型链继承 (1)流程: 1.定义父类型构造函数. 2.给父类型的原型添加方法. 3.定义子类型的构造函数. 4.创建父类型的对象赋值给子类型的原型. 5 ...
- JavaScript 六种继承方式
title: JS的六种继承方式 date: 2017-06-27 05:55:49 tags: JS categories: 学习 --- 继承是面向对象编程中又一非常重要的概念,JavaScrip ...
- JavaScript各种继承方式(二):借用构造函数继承(constructor stealing)
一 原理 在子类的构造函数中,通过call ( ) 或 apply ( ) 的形式,调用父类的构造函数来实现继承. function Fruit(name){ this.name = name; th ...
- 详解JavaScript对象继承方式
一.对象冒充 其原理如下:构造函数使用 this 关键字给所有属性和方法赋值(即采用类声明的构造函数方式).因为构造函数只是一个函数,所以可使 Parent 构造函数成为 Children 的方法,然 ...
- JavaScript对象继承方式
一.对象冒充 其原理如下:构造函数使用 this 关键字给所有属性和方法赋值(即采用类声明的构造函数方式).因为构造函数只是一个函数,所以可使 Parent 构造函数 成为 Children 的方法, ...
- 谈谈JavaScript中继承方式
聊一聊js中的继承 一.简单继承---使用原型赋值的方式继承,将实例化的对象,赋值给子级的原型 父级构造函数 function Parent(param) { this.name = 'parent' ...
随机推荐
- 启用NFS方案(读写分离)
- Java8 流的使用示例
foreach遍历处理 dataList.stream().forEach(index -> sb.append(dataList.get(index) + "',")); ...
- 缓冲区 粘包 029 send 和sendall 的区别 find 和 findall 的区别
一.tcp : 属于长连接 与客户端连接了之后 其他客户端需要等待 要连接另外一个 必须优雅的断开前面这个客户的连接. 二.缓冲区 :为了避免网络传输信号不通畅而是程序一直停留在消息发送状态而不向下进 ...
- linux下FTP服务搭建(1)
1.FTP介绍: FTP (File Transfer Protocol,文件传输协议)主要用来文件传输,尤其适用于大文件传输,提供上传下载功能 FTP官方网站:https://filezilla-p ...
- CENTOS7下安装REDIS4.0.11
拷贝收藏私用,别无他意,原博客地址: https://www.cnblogs.com/zuidongfeng/p/8032505.html 1.安装redis 第一步:下载redis安装包 wget ...
- c++ 封装线程库 2
1.2线程回收: 首先得知道线程的两个状态: Joinable Detached 简单理解,如果一个线程是joinable的状态,那么这样的线程,就必须使用pthread_join来回收,否则程序结束 ...
- SQL---MySQL数据库---试炼
1.需求 user表 temp表 user_temp_salary表 1.1 查找每个人在2018年前2个月的平均工资信息 SELECT b.`name` AS userName,c.name AS ...
- 配置编译器(GCC和GFortran)
平台信息 Description: CentOS Linux release 7.6.1810 (Core) 检查环境 $ gfortran -v $ gcc -v 安装 GCC和Fortran 环境 ...
- javasript数据类型以及如何判断数据类型
在javascript里面一共有5种基本的数据类型,分别是:Number,String,Boolean,Null,Undefined7种引用类型,分别是:Object类型,Array类型,Date类型 ...
- python get_dummies与cut离散化数据