function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError( "Super expression must either be null or a function, not " + typeof superClass ); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : (subClass.__proto__ = superClass);}上面是babel转码后的效果,其中这段代码if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : (subClass.__proto__ = superClass);问题:1.这段代码不加我觉得也算实现继承了吧?看高程里面貌似也没有给子类的构造函数设置__proto__的代码啊?设置跟不设置有多大区别?__proto__印象中不是只有对象才有么?虽然js里面一切皆对象,但是常见的故事new 出来的实例会讲到它的__proto__,函数的__proto__怎么理解?
1 回答
青春有我
TA贡献1784条经验 获得超8个赞
回答下:通过查资料理解
if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass);
这段代码是为了继承父类的静态属性,一般静态属性会加在构造函数上而不是原型上.
添加回答
举报
0/150
提交
取消