Js原型链继承

xiaoxiao2021-02-28  41

关键 :

1.构造函数

2.原型复制 借助空函数 new操作

function Child(props) { Parent.call(this, props); this.grade = props.grade || 1; } function inherits(Child, Parent) { var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; } 小结 JavaScript的原型继承实现方式就是: 1.定义新的构造函数,并在内部用call()调用希望“继承”的构造函数,并绑定this; 2.借助中间函数F实现原型链继承,最好通过封装的inherits函数完成; 3.继续在新的构造函数的原型上定义新方法。
转载请注明原文地址: https://www.6miu.com/read-2626339.html

最新回复(0)