美文网首页
js中实现继承的7种方式

js中实现继承的7种方式

作者: sunnyghx | 来源:发表于2018-03-26 12:21 被阅读95次

我的理解:继承通俗地讲就是子代拥有了父代的比如:地位,金钱,房产等等。在js中,继承就是让一个对象拥有另一个对象的属性和方法。下面就来谈谈怎样实现继承。

(Father代表父类构造函数,Son代表子类构造函数,默认构造函数的方法都是写在原型中,实例化对象共享原型中的方法,避免了内存空间的浪费)

1,原型链继承 (有两种实现方式)

(1)Son.prototype = Father.prototype

弊端:Son.prototype.constructor 指向Father,需要手动更改为Son ;Son的实例化对象只能继承Father原型中的方法,无法继承Father本身的属性。

(2)Son.prototype = new Father()

弊端:Son.prototype.constructor 指向Father,需要手动更改为Son;Son的实例化对象共享Father自身的引用类型属性。什么意思呢?下面举个栗子:

function Father(){ this.name = "zs" ; this.arr = [1,2,3] }

function Son(){ }

Son.prototype = new Father()

var s1 = new Son(), s2 = new Son();

s1.arr.push(5);

console.log(s1.arr)--------> [1,2,3,5]

console.log(s2.arr)--------->[1,2,3,5]

看了这里例子就明白了,Son的实例化对象s1,s2继承了Father的属性arr,但是s1,s2是同时指向这一属性的。

2,借助构造函数继承

function Father(){ this.name = "zs"; this.age=38 };

function Son(){ Father.call( this) / Father.apply(this) }

弊端:Son只能继承Father自身的属性,而无法继承Father原型中的方法。

3,组合式继承

将原型链继承与构造函数结合起来

function Father(){ this.name="zs";this.age=38 }

Father.prototype.sayHi = function(){ alert("hello") }

function Son(){ Father.call(this) }

Son.prototype = new Father()

var s = new Son() ;

弊端:通过Father.call() 和 new Father() ,父类构造函数Father被调用了两次。

4,原型式继承

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

var obj = { name:"zs" , age:18, sayHi:function(){ } }

var newObj = createObj( obj );

newObj继承了obj的属性和方法,但是同样出现了共享父类中引用类型属性的问题。

5,经典继承(Es5中的新语法:存在兼容性问题,需要做浏览器的能力检测)

function create(obj){

if(Object.create){ return Object.create(obj) }

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

}

6,寄生式继承(类似于原型式继承)

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

function createObj2(o){ var obj = createObj(o) ; obj.sayHi = function(){ } return obj }

var obj = { name:"zs" , age:18, sayHi:function(){ } }

var newObj = createObj2(obj)

newObj继承了obj的属性和方法,但是同样出现了共享父类中引用类型属性的问题。

7,寄生组合式继承(组合继承+寄生继承)

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

function inheritPrototype(Child, Father) {

var prototype = object(Father.prototype);//创建对象

prototype.constructor = Child;//增强对象

Child.prototype = prototype;//指定对象 }

function Father(name) {

this.name = name;

this.arr = [1, 2, 3, 4]; }

Father.prototype.sayName = function () { console.log("父类原型" + this.name); }

function Child(name, age) { Father.call(this, name); this.age = age; }

inheritPrototype(Child, Father)

Child.prototype.sayAge = function () { console.log(this.age); }

var child1 = new Child() , child2 = new Child();

child1.arr.push(5) ------> [1,2,3,4,5]

child2.arr ------> [1,2,3,4].

优点:可以多重继承 解决两次调用 解决实例共享引用类型的问题 原型链保持不变

作者:Anna_Hu
链接:https://www.jianshu.com/p/0580a641eee7
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关文章

  • JS继承

    JS中的继承 许多OO语言都支持两种继承方式:接口继承和实现继承; 因为JS中没有类和接口的概念 , 所以JS不支...

  • JS中基于原型实现继承

    本文不会对于JS中的继承机制进行深入研究,只聊js中基于原型实现继承的方式,如果需要了解JS中的继承机制及其相关背...

  • js中实现继承的方式

    原型继承 借用构造函数继承 组合继承:

  • js实现继承的几种方式

    如何实现继承? js中实现继承的方式主要是通过原型链完成的。了解原型链的相关信息可以点这里 javascript中...

  • JS继承的实现的几种方式

    前言 JS作为面向对象的弱类型语言,继承也是非常强大的特性之一,那么如何在JS实现继承呢? JS继承的实现方式 既...

  • js中的继承-原型与原型链

    面向对象的语言支持两种继承方式,接口继承和实现继承js无法实现接口继承,只支持实现继承,主要通过原型链来实现。具体...

  • js继承之原型链继承

    面向对象编程都会涉及到继承这个概念,JS中实现继承的方式主要是通过原型链的方法。

  • js基础之实现继承的几种方式

    js 实现继承的方式有: 原型链继承; 构造函数继承; 组合继承(原型链继承 + 构造函数继承)(最常用);(原型...

  • JS实现继承方式

    js中的继承: Object是所有对象的父级/父类型/超类型,js中所有的对象都直接或间接的继承自Object. ...

  • JavaScript 10

    js继承的概念 1.通过原型链方式实现继承(对象间的继承) 2.类式继承(构造函数间的继承) 由于js不像Java...

网友评论

      本文标题:js中实现继承的7种方式

      本文链接:https://www.haomeiwen.com/subject/nlxrcftx.html