理解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 相关文章推荐
关于跨站脚本攻击问题
Dec 22 Javascript
jQuery Tools tab(幻灯片)
Jul 14 Javascript
jquery.cookie用法详细解析
Dec 18 Javascript
jQuery添加/改变/移除CSS类及判断是否已经存在CSS
Aug 20 Javascript
javascript实现图片自动和可控的轮播切换特效
Apr 13 Javascript
JQuery勾选指定name的复选框集合并显示的方法
May 18 Javascript
javascript中SetInterval与setTimeout的定时器用法
Aug 24 Javascript
解决JS无法调用Controller问题的方法
Dec 31 Javascript
基于javascript实现checkbox复选框实例代码
Jan 28 Javascript
jQuery滚动条美化插件nicescroll简单用法示例
Apr 18 jQuery
Vue前后端不同端口的实现方法
Sep 19 Javascript
解决layui的input独占一行的问题
Sep 10 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怎样调用MSSQL的存储过程
2006/10/09 PHP
PHP Mysqli 常用代码集合
2016/11/12 PHP
php定期拉取数据对比方法实例
2019/09/22 PHP
详解laravel passport OAuth2.0的4种模式
2019/11/04 PHP
js function定义函数使用心得
2010/04/15 Javascript
基于jQuery全屏焦点图左右切换插件responsiveslides
2015/09/07 Javascript
让你一句话理解闭包(简单易懂)
2016/06/03 Javascript
把json格式的字符串转换成javascript对象或数组的方法总结
2016/11/03 Javascript
Javascript 实现放大镜效果实例详解
2016/12/03 Javascript
jquery UI Datepicker时间控件冲突问题解决
2016/12/16 Javascript
jQuery Validate 相关参数及常用的自定义验证规则
2017/03/06 Javascript
nodejs 子进程正确的打开方式
2017/07/03 NodeJs
初学者AngularJS的环境搭建过程
2017/10/27 Javascript
Vue cli 引入第三方JS和CSS的常用方法分享
2018/01/20 Javascript
微信小程序实现弹出层效果
2020/05/26 Javascript
详解关于微信setData回调函数中的坑
2019/02/18 Javascript
详解基于iview-ui的导航栏路径(面包屑)配置
2019/02/22 Javascript
vue2 拖动排序 vuedraggable组件的实现
2019/08/08 Javascript
JavaScript语法约定和程序调试原理解析
2020/11/03 Javascript
[02:25]专访DOTA2负责人Erik 国际邀请赛暂不会离开西雅
2014/07/21 DOTA
python多进程实现进程间通信实例
2017/11/24 Python
Python实现PS滤镜特效之扇形变换效果示例
2018/01/26 Python
linux环境下Django的安装配置详解
2019/07/22 Python
python实现的自动发送消息功能详解
2019/08/15 Python
Python turtle库绘制菱形的3种方式小结
2019/11/23 Python
torchxrayvision包安装过程(附pytorch1.6cpu版安装)
2020/08/26 Python
Python爬虫教程知识点总结
2020/10/19 Python
python 实现围棋游戏(纯tkinter gui)
2020/11/13 Python
最新远光软件笔试题面试题内容
2013/11/08 面试题
在校生自我鉴定
2014/01/23 职场文书
党的群众路线对照检查材料思想汇报(学校)
2014/10/04 职场文书
交通事故赔偿协议书怎么写
2014/10/04 职场文书
党员专题组织生活会发言材料
2014/10/17 职场文书
医生辞职信范文
2015/03/02 职场文书
交通事故被告答辩状
2015/05/22 职场文书
2019年销售人员的职业生涯规划书
2019/03/25 职场文书