Javascript继承的4种办法

85 阅读1分钟

本文是类似笔记的功能

1.原型继承: 子类的原型等于父类的实例 Child.prototype = new Parent 子类实例能用子类私有的和原型上公有的(非拷贝式) 最终其实是 Child.prototype.proto = Parent.prototype 原型继承.png

2.call继承,子类里面把父类当做普通函数执行 (拷贝式) 只能继承父类私有的 无法继承父类公共的 他跟父类原型链没关系 Child { Parent.call(this) }

3.寄生组合式继承(另类原型继承+call) Child { Parent.call(this) } Child.prototype.proto = Parent.prototype 这句话IE不支持 可以用下一句代替

Child.prototype = Object.create(Parent.prototype)

Child.prototype.constructor = Child

Object.setPrototypeOf(Child, Parent) 继承父类的静态方法

4.ES5类的继承 super类似于call继承