抽象类中子类为什么不能调用父类的非抽象方法?
function DetectorBase() { throw new Error('Abstract class can not be invoked directly!'); } DetectorBase.detect = function() { console.log('Detection starting...'); } DetectorBase.stop = function() { console.log('Detection stopped.'); }; DetectorBase.init = function() { throw new Error('Error'); } //var d = new DetectorBase();// Uncaught Error: Abstract class can not be invoked directly! function LinkDetector() {} LinkDetector.prototype = Object.create(DetectorBase.prototype); LinkDetector.prototype.constructor = LinkDetector; var l = new LinkDetector(); console.log(l); //LinkDetector {}__proto__: LinkDetector l.detect(); //Uncaught TypeError: l.detect is not a function l.init(); //Uncaught TypeError: l.init is not a function
主要是倒数第二行代码。为什么会报错,这里不是应该执行的吗?控制台应该输出 Detection starting... 吗?
---
我把老师的代码补全,实例化,调用了一下,为什么没有成功呢?