ES5 原型继承过程中为什么需要创建一个临时函数?
题目来源及自己的思路
相关代码
function inherit(C,P) {
var F = new Function(); // 临时构造函数
F.prototype = P.prototype;
C.super = P; // 使得子类能够获得对父类的引用
C.prototype = new F(); // 使得子类的原型对象__proto__指向父类的原型对象,从而实现继承原型方法
C.prototype.constructor = C; // 使得子类的constructor指针重新指向子类的构造函数
}
inherit(ChildrenClass,ParentClass)
创建一个临时函数目的是为了隔离C的prototype和P的prototype,这样假如C的prototype有引用对象的时候,对其进行修改时不会影响到P的prototype。