理解js对象继承的N种模式


Posted in Javascript onJanuary 25, 2016

本文分享了js对象继承的N种模式,供大家参考。

一、原型链继承

function Person(){};

Person.prototype = {
  constructor: Person,
  name: "Oliver"
};
    
function People(){};

People.prototype = new Person();
People.prototype.constructor = People;
People.prototype.sayName = function(){
  return this.name;
};

var ins = new People();

console.log(ins.sayName());

二、借用构造函数(伪造对象,经典继承)

1、无参数

function SuperType(){
  this.color = ["red","yellow","white"];
}
function SubType(){
  SuperType.call(this);
}

var instance1 = new SubType();
var instance2 = new SubType();

instance1.color.pop();
console.log(instance1.color); //["red", "yellow"]
console.log(instance2.color); //["red", "yellow", "white"]

2、有参数

function SuperType(name){
  this.name = name;
  this.number = [21,32,14,1];
}
function SubType(name,age){
  SuperType.call(this,name);
  this.age = age;
}

var instance1 = new SubType("Oliver",18);
var instance2 = new SubType("Troy",24);

instance2.number.pop();

console.log(instance1.name + instance1.age + instance1.number); //Oliver1821,32,14,1
console.log(instance2.name + instance2.age + instance2.number); //Troy2421,32,14

三、组合继承(伪经典继承)

1、无参数

function SuperType(){
  this.color = ["red","yellow","white"];
}
SuperType.prototype.sayColor = function(){
  return this.color;
};

function SubType(){
  SuperType.call(this);
  this.number = 321;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayNumber = function(){
  return this.number;
};

var instance1 = new SubType();
var instance2 = new SubType();

instance2.color.pop();
console.log(instance1.color + instance1.number); //red,yellow,white321
console.log(instance2.color + instance2.number); //red,yellow321

2、有参数

function SuperType(name){
  this.name = name;
  this.number = [32,1342,11,1];
}
SuperType.prototype.sayName = function(){
  return this.name;
};

function SubType(name,age){
  SuperType.call(this,name);
  this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
  return this.age;
};

var instance1 = new SubType("Oliver",18);
var instance2 = new SubType("Troy",24);

instance2.number.pop();
console.log(instance1.sayName() + instance1.sayAge() + instance1.number); //Oliver1832,1342,11,1
console.log(instance2.sayName() + instance2.sayAge() + instance2.number); //Troy2432,1342,11

三、寄生组合式继承(引用类型最理想的范式)

function inheritPrototype(subType,superType){
  var prototype = Object(superType.prototype);
  prototype.constructor = subType;
  subType.prototype = prototype;
}

function SuperType(name){
  this.name = name;
  this.number = [321,321,43];
}
SuperType.prototype.sayName = function(){
  return this.name;
};

function SubType(name,age){
  SuperType.call(this,name);
  this.age = age;
}
inheritPrototype(SubType,SuperType);
SubType.prototype.sayAge = function(){
  return this.age;
};

var instance1 = new SubType("Oliver",18);
var instance2 = new SubType("Troy",24);
instance2.number.pop();

console.log(instance1.sayName() + instance1.sayAge() + instance1.number); //Oliver18321,321,43
console.log(instance2.sayName() + instance2.sayAge() + instance2.number); //Troy24321,321

或者可以把inheritPrototype 函数写成下面这样:

function inheritPrototype(SubType,SuperType){
  SubType.prototype = new SuperType();
  SubType.prototype.constructor = SubType;
}

四、原型式继承(用于共享引用类型的值,与寄生式类似)

1、传统版(先定义object() 函数,再继承)

function object(o){
  function F(){};
  F.prototype = o;
  return new F();
}

var SuperType = {
  name: "Oliver",
  number: [321,321,4532,1]
};

var SubType1 = object(SuperType);
var SubType2 = object(SuperType);

SubType1.name = "Troy";
SubType1.number.pop();

SubType2.name = "Alice";
SubType2.number.pop();

console.log(SubType1.name + SubType2.name + SubType1.number + SubType2.number + SuperType.name + SuperType.number); //TroyAlice321,321321,321Oliver321,321

ECMAScript 5 版(直接用Object.create(),再继承)

var SuperType = {
  name: "Oliver",
  number: [321,321,4532,1]
};

var SubType1 = Object.create(SuperType); //省略了定义object()函数
var SubType2 = Object.create(SuperType);

SubType1.name = "Troy";
SubType1.number.pop();

SubType2.name = "Alice";
SubType2.number.pop();

console.log(SubType1.name + SubType2.name + SubType1.number + SubType2.number + SuperType.name + SuperType.number); //TroyAlice321,321321,321Oliver321,321

ECMAScript 5 简写版(定义Object.create()的第二个参数,再继承)

var SuperType = {
  name: "Oliver",
  number: [321,321,4532,1]
};

var SubType1 = Object.create(SuperType,{
  name: {
    value : "Troy"
  }
});
var SubType2 = Object.create(SuperType,{
  name: {
    value : "Alice"
  }
});

SubType1.number.pop();
SubType2.number.pop();

console.log(SubType1.name + SubType2.name + SubType1.number + SubType2.number + SuperType.name + SuperType.number); //TroyAlice321,321321,321Oliver321,321

寄生式继承(用于共享引用类型的值,与原型式类似)

function createAnother(original){
  var clone = Object(original);
  clone.sayHi = function(){
    return "Hi";
  };
  return clone;
}

var person = {
  name: "Oliver",
  number: [13,21,31,1]
};

var anotherPerson = createAnother(person);
anotherPerson.number.pop();

console.log(anotherPerson.sayHi() + anotherPerson.number); //Hi13,21,31
console.log(person.number); //13,21,31

以上就是本文的全部内容,希望对大家的学习有所帮助。

Javascript 相关文章推荐
document 和 document.all 分别什么时候用
Jun 22 Javascript
javascript 字符串连接的性能问题(多浏览器)
Nov 18 Javascript
javascript 表单验证常见正则
Sep 28 Javascript
js 实现浏览历史记录示例
Apr 20 Javascript
js使用cookie记录用户名的方法
Nov 26 Javascript
Bootstrap Metronic完全响应式管理模板学习笔记
Jul 08 Javascript
js实现为a标签添加事件的方法(使用闭包循环)
Aug 02 Javascript
canvas实现手机端用来上传用户头像的代码
Oct 20 Javascript
JavaScript数据结构之二叉树的删除算法示例
Apr 13 Javascript
vue实现引入本地json的方法分析
Jul 12 Javascript
详解ECMAScript2019/ES10新属性
Dec 06 Javascript
ES6 Object.assign()的用法及其使用
Jan 18 Javascript
解决js函数闭包内存泄露问题的办法
Jan 25 #Javascript
JavaScript数据类型学习笔记
Jan 25 #Javascript
分步解析JavaScript实现tab选项卡自动切换功能
Jan 25 #Javascript
jQuery form 表单验证插件(fieldValue)校验表单
Jan 24 #Javascript
Jquery实现纵向横向菜单
Jan 24 #Javascript
JavaScript、jQuery与Ajax的关系
Jan 24 #Javascript
JavaScript jquery及AJAX小结
Jan 24 #Javascript
You might like
php你的验证码安全码?
2007/01/02 PHP
PHP中feof()函数实例测试
2014/08/23 PHP
PHP对象相关知识总结
2017/04/09 PHP
关于Laravel-admin的基础用法总结和自定义model详解
2019/10/08 PHP
ExtJS GridPanel 根据条件改变字体颜色
2010/03/08 Javascript
将文本输入框内容加入表中的js代码
2013/08/18 Javascript
JS实现黑色大气的二级导航菜单效果
2015/09/18 Javascript
javascript实现状态栏中文字动态显示的方法
2015/10/20 Javascript
理解javascript异步编程
2016/01/27 Javascript
jQuery qrcode生成二维码的方法
2016/04/03 Javascript
JavaScript实现显示函数调用堆栈的方法
2016/04/21 Javascript
第五篇Bootstrap 排版
2016/06/21 Javascript
Bootstrap分页插件之Bootstrap Paginator实例详解
2016/10/15 Javascript
浅谈$_FILES数组为空的原因
2017/02/16 Javascript
详解Vuejs2.0之异步跨域请求
2017/04/20 Javascript
vue高德地图之玩转周边
2017/06/16 Javascript
微信小程序实现左右联动的实战记录
2018/07/05 Javascript
JS/HTML5游戏常用算法之追踪算法实例详解
2018/12/12 Javascript
150行代码带你实现微信小程序中的数据侦听
2019/05/17 Javascript
vue中实现点击按钮滚动到页面对应位置的方法(使用c3平滑属性实现)
2019/12/29 Javascript
[02:11]2016国际邀请赛中国区预选赛最美TA采访现场玩家
2016/06/28 DOTA
pygame播放音乐的方法
2015/05/19 Python
Python中%是什么意思?python中百分号如何使用?
2018/03/20 Python
Python函数装饰器常见使用方法实例详解
2019/03/30 Python
关于tensorflow的几种参数初始化方法小结
2020/01/04 Python
PyTorch 普通卷积和空洞卷积实例
2020/01/07 Python
使用keras框架cnn+ctc_loss识别不定长字符图片操作
2020/06/29 Python
Aquatalia官网:意大利著名鞋履品牌
2019/09/26 全球购物
英国领先的餐饮折扣俱乐部:Gourmet Society
2020/07/26 全球购物
美工的岗位职责
2013/11/14 职场文书
模具专业毕业生自荐书范文
2014/02/19 职场文书
小学语文课后反思精选
2014/04/25 职场文书
金融专业求职信
2014/08/05 职场文书
2014年公务员退休工资改革方案
2014/10/01 职场文书
2014年村计划生育工作总结
2014/11/14 职场文书
2015年企业新年寄语
2014/12/08 职场文书