面向对象的JavaScript系列二,继承
1.原型链
function SuperType(){
this.property = true;
} SuperType.prototype.getSuperValue = function(){
return this.property;
}; function SubType(){
this.subproperty = false;
} //inherit from SuperType
SubType.prototype = new SuperType(); SubType.prototype.getSubValue = function (){
return this.subproperty;
}; var instance = new SubType();
alert(instance.getSuperValue()); //true // 原型链上查询,首先是function就是Object实例出来,然后SubType的prototype是SuperType的实例,最后是instance是SubType的实例。
alert(instance instanceof Object); //true
alert(instance instanceof SuperType); //true
alert(instance instanceof SubType); //true alert(Object.prototype.isPrototypeOf(instance)); //true
alert(SuperType.prototype.isPrototypeOf(instance)); //true
alert(SubType.prototype.isPrototypeOf(instance)); //true
1.1原型链的问题(很少用这种方法来实际开发)
function SuperType(){
this.colors = ["red", "blue", "green"];
} function SubType(){
} //inherit from SuperType
SubType.prototype = new SuperType(); var instance1 = new SubType();
// 增加了black,导致原型属性也增加了black
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black" var instance2 = new SubType();
alert(instance2.colors); //"red,blue,green,black"
2.借用构造函数(实际开发,也不会这么用)
function SuperType(name){
this.name = name;
} function SubType(){
//inherit from SuperType passing in an argument
SuperType.call(this, "Nicholas"); //instance property
this.age = 29;
} var instance = new SubType();
alert(instance.name); //"Nicholas";
alert(instance.age); //
3.组合继承,将原型链和借用构造函数的技术组合起来,扬长避短
function SuperType(name){
this.name = name;
this.colors = ["red", "blue", "green"];
} SuperType.prototype.sayName = function(){
alert(this.name);
}; function SubType(name, age){
SuperType.call(this, name); this.age = age;
} SubType.prototype = new SuperType(); SubType.prototype.sayAge = function(){
alert(this.age);
}; var instance1 = new SubType("Nicholas", 29);
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
instance1.sayName(); //"Nicholas";
instance1.sayAge(); // var instance2 = new SubType("Greg", 27);
alert(instance2.colors); //"red,blue,green"
instance2.sayName(); //"Greg";
instance2.sayAge(); //
4.原型式继承
function object(o){
function F(){}
F.prototype = o;
return new F();
} var person = {
name: "Nicholas",
friends: ["Shelby", "Court", "Van"]
}; var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob"); var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie"); alert(person.friends); //"Shelby,Court,Van,Rob,Barbie"
4.1原型式继承 ECMA5版本
var person = {
name: "Nicholas",
friends: ["Shelby", "Court", "Van"]
}; var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob"); var yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie"); alert(person.friends); //"Shelby,Court,Van,Rob,Barbie"
5.寄生组合式继承(当前最佳实践)
function object(o){
function F(){}
F.prototype = o;
return new F();
} function inheritPrototype(subType, superType){
var prototype = object(superType.prototype); //create object
prototype.constructor = subType; //augment object 如果不修复constructor,则指向SuperType
subType.prototype = prototype; //assign object
} function SuperType(name){
this.name = name;
this.colors = ["red", "blue", "green"];
} SuperType.prototype.sayName = function(){
alert(this.name);
}; function SubType(name, age){
SuperType.call(this, name); this.age = age;
} inheritPrototype(SubType, SuperType); SubType.prototype.sayAge = function(){
alert(this.age);
}; var instance1 = new SubType("Nicholas", 29);
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
instance1.sayName(); //"Nicholas";
instance1.sayAge(); // var instance2 = new SubType("Greg", 27);
alert(instance2.colors); //"red,blue,green"
instance2.sayName(); //"Greg";
instance2.sayAge(); //
面向对象的JavaScript系列二,继承的更多相关文章
- 面向对象的JavaScript系列一,创建对象
1.最简单的创建对象方法 var person = new Object(); person.name = "sam wu"; person.age = 25; person.jo ...
- JavaScript设计模式基础之面向对象的JavaScript(二)
多态 多态的实际含义:同一操作作用与不同的对象上面,可以产生不同的解释和不同的执行结果,就是说,给不同的对象发送同一个消息 的时候,这些对象会根据这个消息分别给出不同的反馈 代码如下: class D ...
- (二)Javascript面向对象编程:构造函数的继承
Javascript面向对象编程:构造函数的继承 这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承 ...
- JavaScript系列--浅析原型链与继承
一.前言 继承是面向对象(OOP)语言中的一个最为人津津乐道的概念.许多面对对象(OOP)语言都支持两种继承方式::接口继承 和 实现继承 . 接口继承只继承方法签名,而实现继承则继承实际的方法.由于 ...
- 前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型
前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型 前言(题外话): 有人说拖延症是一个绝症,哎呀治不好了.先不说这是一个每个人都多多少少会有的,也不管它究竟对生活有多么大的 ...
- 面向对象的Javascript(5):继承
在小项目中对于JavaScript使用,只要写几个function就行了.但在大型项目中,尤其是在开发追求 良好的用户体验的网站中,如SNS,就会 用到大量的JavaScrpt,有时JavaScrip ...
- (一)我的Javascript系列:Javascript的面向对象旅程(上)
今宵酒醒何处,杨柳岸,晓风残月 导引 我的JavaScript系列文章是我自己对JavaScript语言的感悟所撰写的系列文章.现在还没有写完.目前一共出了下面的系列: (三)我的JavaScript ...
- javascript面向对象之Javascript 继承
转自原文javascript面向对象之Javascript 继承 在JavaScript中实现继承可以有多种方法,下面说两种常见的. 一,call 继承 先定义一个“人”类 //人类 Person=f ...
- JavaScript 面向对象程序设计(下)——继承与多态 【转】
JavaScript 面向对象程序设计(下)--继承与多态 前面我们讨论了如何在 JavaScript 语言中实现对私有实例成员.公有实例成员.私有静态成员.公有静态成员和静态类的封装.这次我们来讨论 ...
随机推荐
- 161116、springmvc自己实现防止表单重复提交(基于注解)
原理:在去某个页面直接生成一个随机数(这里使用的是UUID)并放入session中,用户提交表单时将这个随机数传入服务端与session中的值进行比较,如果不不存在或不相等,则认为是重复提交:如果相等 ...
- win10 python nltk安装
主要是参照http://www.tuicool.com/articles/VFf6Bza
- MySQL Replication浅析
MySQL Replication是MySQL非常出色的一个功能,该功能将一个MySQL实例中的数据复制到另一个MySQL实例中.整个过程是异步进行的,但由于其高效的性能设计,复制的延时非常小.MyS ...
- centos7.1-64bit延时截屏
centos自带了截屏的软件,而且还能延时截屏. 在桌面左上角的应用程序菜单里: “应用程序”->“工具”->“截图” 设定延时秒数,点击“截图”按钮,开始截图. 完.
- C#Web编程
1.Web服务器控件可以包含服务器上调用的事件处理程序.只有送回服务器时,才在服务器上触发事件.把autoPostBack设置为true,事件将立即传给服务器.这样就会使客户端调用Javascript ...
- oracle 存储过程和函数例子
关于 游标 if,for 的例子 create or replace procedure peace_if is cursor var_c is select * from grade; begin ...
- move语义和右值引用
C++11支持move语义,用以避免非必要拷贝和临时对象. 具体内容见收藏中的“C++右值引用” .
- jquery ui 常用(二)(对话框 | 旋转器 | 工具提示框(表单) | 特效(百叶窗) )
一.添加信息的对话框 http://www.w3cschool.cc/try/tryit.php?filename=jqueryui-example-dialog-modal-form. 模态表单 ...
- xheditor上传图片配置
二. 源码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Xeditor.a ...
- Web应用程序状态管理(上)
一:概述Http协议使用的是无状态连接:客户浏览器与服务器建立连接-发出请求-得到 响应-关闭连接.话句话说,连接只针对一个请求/响应.由于连接不会持久保留 所以容器认不出做第二个请求的客户与做前一个 ...