时间:2021-05-18
本文分享了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,1console.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,white321console.log(instance2.color + instance2.number); //red,yellow3212、有参数
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;}四、原型式继承(用于共享引用类型的值,与寄生式类似)
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,321ECMAScript 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,321ECMAScript 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寄生式继承(用于共享引用类型的值,与原型式类似)
以上就是本文的全部内容,希望对大家的学习有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
温馨提示:想要更好的理解JS继承方式,须了解构造函数、原型对象、实例化对象、原型链等概念第一种:原型链继承利用原型链的特点进行继承functionParent(
本文实例讲述了JS实现面向对象继承的5种方式。分享给大家供大家参考,具体如下:js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明确的继承
整理一下js面向对象中的封装和继承。1.封装 js中封装有很多种实现方式,这里列出常用的几种。1.1原始模式生成对象 直接将我们的成员写入对象中,用函数
装饰设计模式每种设都有其独特的应用场景和解决问题的方式,装饰设计模式是动态的为对象添加新的功能,是一种用于代替继承的技术,无需通过继承增加子类就能扩展对象的新功
前言JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一。那么如何在JS中实现继承呢?让我们拭目以待。JS继承的实现方式既然要实现继承,那么首先我们得有一