1. 原型链继承 (缺点:子类继承父类的引用类型的属性值会在各个实例中共享,创建子类实例时无法向父类构造函数传递参数)

// 定义父类构造函数
function SuperClass(father, mother){
this.father = father;
this.mother = mother;
this.brothers = ['jack', 'mike'];
}
// 给父类中添加方法
SuperClass.prototype.getFather = function(){
return this.father;
} // 定义子类构造函数
function SubClass(name, age){
this.name = name;
this.age = age;
}
// 继承父类
SubClass.prototype = new SuperClass('perter','nancy'); // 添加新方法
SubClass.prototype.getName = function(){
return this.name;
};
// 重写父类中的方法
SubClass.prototype.getFather = function(){
return null;
} // 实例化子类
var obj = new SubClass('leon', 30);

2. 借用构造函数继承(优点:子类继承的数据不会共享,可以在实例化子类时对父类进行传参;缺点:方法需要在构造函数中定义,以及重写)

// 定义父类构造函数
function SuperClass(father, mother){
this.father = father;
this.mother = mother;
this.brothers = ['jack', 'mike'];
this.getFather = function(){ // 需要子类继承的方法一定得在构造函数中定义
return this.father;
}
} // 定义子类构造函数, 并调用父构造函数实现继承
function SubClass(name, age,father, mother){
SuperClass.call(this, father, mother);
this.name = name;
this.age = age;
    this.getFather = function(){  // 只能在构造函数中重写父类中的方法
return null;
}
}
// 添加新方法
SubClass.prototype.getName = function(){
return this.name;
}; // 实例化子类
var obj = new SubClass('leon', 30, 'peter', 'nancy');

3. 组合继承 (融合原型链继承与借用构造函数继承)

// 定义父类构造函数
function SuperClass(father, mother){
this.father = father;
this.mother = mother;
this.brothers = ['jack', 'mike'];
}
// 添加父类方法
SuperClass.prototype.getFather = function(){
return this.father;
}; // 定义子类构造函数, 并调用父构造函数实现属性继承
function SubClass(name, age,father, mother){
SuperClass.call(this, father, mother);
this.name = name;
this.age = age;
} // 通过实例化父类作为子类原型对象,实现方法继承
SubClass.prototype = new SuperClass();
SubClass.prototype.constructor = SubClass; // 添加新方法
SubClass.prototype.getName = function(){
return this.name;
};
// 重写父类中的方法
SubClass.prototype.getFather = function(){
return null;
}; // 实例化子类
var obj = new SubClass('leon', 30, 'peter', 'nancy');

4. 原型式继承 (基于一个给定的对象,创建一个相似的对象, 相当于es5中的Object.create() 方法)

// 实现继承的函数
function object(o){
function F(){}
F.prototype = o;
return new F();
} // 定义基础对象
var baseObj = {
name: 'john',
age: 20,
getName: function(){
return this.name;
}
} // 创建新对象
var newObj = object( baseObj);
newObj.name = 'leon';
newObj.getName();

5. 寄生式继承 (封装原型式继承,并为新创建对象添加新的属性和方法,缺点:新添加的方法无法共享)

// 实现继承的函数
function object(o){
function F(){}
F.prototype = o;
return new F();
} // 封装继承过程的函数
function createObj(obj){
var newObj = object(obj);
newObj.getAge = function(){
return this.age;
}
return newObj;
} // 定义基础对象
var baseObj = {
name: 'john',
age: 20,
getName: function(){
return this.name;
}
} // 创建新对象
var obj = createObj( baseObj);
obj.name = 'leon';
obj.getName();
obj.getAge();

6. 寄生组合式继承 (改进组合式继承,将原型方法的继承改为利用寄生式继承的方法进行继承,避免了在prototype上产生不必想的属性值,以及多次调用父类构造函数的情况)

// 定义父类构造函数
function SuperClass(father, mother){
this.father = father;
this.mother = mother;
this.brothers = ['jack', 'mike'];
}
// 添加父类方法
SuperClass.prototype.getFather = function(){
return this.father;
}; // 定义子类构造函数, 并调用父构造函数实现属性继承
function SubClass(name, age,father, mother){
SuperClass.call(this, father, mother);
this.name = name;
this.age = age;
} // 继承原型的函数
function inheritPrototype(SubClass, SuperClass){
var prototype = object(SuperClass.prototype);
prototype.constructor = SubClass;
SubClass.prototype = prototype;
} // 通过调用原型继承函数,实现方法继承
inheritPrototype(SubClass, SuperClass); // 添加新方法
SubClass.prototype.getName = function(){
return this.name;
}; // 重写父类中的方法
SubClass.prototype.getFather = function(){
return null;
}; // 实例化子类
var obj = new SubClass('leon', 30, 'peter', 'nancy');

javascript 高级编程系列 - 继承的更多相关文章

  1. javascript 高级编程系列 - 函数

    一.函数创建 1. 函数声明 (出现在全局作用域,或局部作用域) function add (a, b) { return a + b; } function add(a, b) { return a ...

  2. javascript 高级编程系列 - 基本数据类型

    javascript中的基本数据类型包括: Undefined, Null, Boolean, Number, String 5种数据类型 1. Undefined 类型 (只有一个值 undefin ...

  3. javascript 高级编程系列 - 创建对象

    1. 工厂模式 function createPerson(name, age) { var obj = {}; obj.name = name; obj.age = age; obj.getName ...

  4. javascript高级编程笔记01(基本概念)

    1.在html中使用JavaScript 1.  <script> 元素 <script>定义了下列6个属性: async:可选,异步下载外部脚本文件. charset:可选, ...

  5. JavaScript高级编程———JSON

    JavaScript高级编程———JSON < script > /*JSON的语法可以表达一下三种类型的值 简单值:使用与javas相同的语法,可以在JSON中表达字符串.数值.布尔值和 ...

  6. JavaScript高级编程———基本包装类型String和单体内置对象Math

    JavaScript高级编程———基本包装类型和单体内置对象 <script> var stringObject = new String("hello world") ...

  7. JavaScript高级编程——Date类型

    JavaScript高级编程——Date类型 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ...

  8. JavaScript高级编程——Array数组迭代(every()、filter()、foreach()、map()、some(),归并(reduce() 和reduceRight() ))

    JavaScript高级编程——Array数组迭代(every().filter().foreach().map().some(),归并(reduce() 和reduceRight() )) < ...

  9. JavaScript高级编程——引用类型、Array数组使用、栈方法

    JavaScript高级编程——引用类型.Array数组使用.栈方法 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999 ...

随机推荐

  1. 【bzoj2563】阿狸和桃子的游戏 贪心

    题目描述 阿狸和桃子正在玩一个游戏,游戏是在一个带权图G=(V, E)上进行的,设节点权值为w(v),边权为c(e).游戏规则是这样的:1. 阿狸和桃子轮流将图中的顶点染色,阿狸会将顶点染成红色,桃子 ...

  2. 【bzoj2561】最小生成树 网络流最小割

    题目描述 给定一个边带正权的连通无向图G=(V,E),其中N=|V|,M=|E|,N个点从1到N依次编号,给定三个正整数u,v,和L (u≠v),假设现在加入一条边权为L的边(u,v),那么需要删掉最 ...

  3. Spring Boot 必须先说说 Spring 框架!

    现在 Spring Boot 非常火,各种技术文章,各种付费教程,多如牛毛,可能还有些不知道 Spring Boot 的,那它到底是什么呢?有什么用?今天给大家详细介绍一下. Spring Boot ...

  4. 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)

    题目1 : String reorder 时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description For this question, your program ...

  5. NOI2016 区间 【线段树】

    题目 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间,使得这 m个区间共同包含至少一个位置.换句话说,就是使得存在一个 x,使得对于每一个被 ...

  6. Zygote原理学习

    1 zygote分析 1.1 简介 Zygote本身是一个NATIVE层的应用程序,与驱动.内核无关.前面已经介绍过了,zygote由init进程根据init.rc配置文件创建.其实本质上来说,zyg ...

  7. 小程序语音红包开发中 汉字转拼音的问题 微信小程序红包开发遇到的坑

    公司最近在开发微信小程序的红包功能,语音红包需要用到文字转拼音的功能. 之前介绍过怎么将中文的汉字转为拼音的,具体看下面这篇文章. 微信语音红包小程序开发如何提高精准度 红包小程序语音识别精准度 微信 ...

  8. mybatis foreach Map(String,List)类型

    <select id="queryList" resultType="com.performancetest.modules.ptest.entity.Stress ...

  9. 编写webconfig连接串与使用(新)

    原文发布时间为:2008-07-27 -- 来源于本人的百度文章 [由搬家工具导入] webconfig 中<appSettings/> 得后面代码添加如下: <appSetting ...

  10. Enable and Use Remote Commands in Windows PowerShell

    The Windows PowerShell remoting features are supported by the WS-Management protocol and the Windows ...