博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
细节:js 对象继承的几种模式举例
阅读量:7212 次
发布时间:2019-06-29

本文共 5051 字,大约阅读时间需要 16 分钟。

原型链继承

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());

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

无参数

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"]

有参数

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,1console.log(instance2.name + instance2.age + instance2.number); //Troy2421,32,14

组合继承(伪经典继承)

无参数

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,white321console.log(instance2.color + instance2.number); //red,yellow321

有参数

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,1console.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,43console.log(instance2.sayName() + instance2.sayAge() + instance2.number); //Troy24321,321

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

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

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

传统版(先定义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,31console.log(person.number); //13,21,31

转载地址:http://icgum.baihongyu.com/

你可能感兴趣的文章
前端性能优化 —— 项目瘦身
查看>>
全球人形机器人接连突破 拟人度越来越高
查看>>
vue按需加载
查看>>
创成汇2019年参加创新创业大赛都能获得什么?
查看>>
vue双向数据绑定原理
查看>>
美研究最新生物活性玻璃 可消灭致命的细菌
查看>>
内部类
查看>>
Vue中数组赋值问题
查看>>
APK path is not specified for module
查看>>
Linux运维宝典:最常用的150个命令汇总
查看>>
使用RecycleView实现无限滚动的日历
查看>>
Golang Failpoint 的设计与实现
查看>>
小微贷是美团的上坡之路?
查看>>
js 将线性数据转为树形
查看>>
java B2B2C 源码 多级分销Springcloud多租户电子商城系统- 整合企业架构的技术点(二)...
查看>>
微信小程序
查看>>
区块链+金融
查看>>
阿里开发者招聘节 | 面试题14:如何实现两金额数据相加(最多小数点两位)...
查看>>
一些不错的文章
查看>>
Python爬虫常见面试题(二)
查看>>