问答详情
源自:9-2 实践(探测器)

按源代码复制 执行报错, 求解答

http://img.mukewang.com/5df367f60001cb1405630125.jpg

//=实战 探测器
!function(global) {
function DetectorBase(configs) {
if (!this instanceof DetectorBase) {
throw new Error('Do not invoke without new.');
}
this.configs = configs;
this.analyze();
}
DetectorBase.prototype.dectect = function () {
throw new Error('Not implemented!');
}
DetectorBase.prototype.analyze = function() {
console.log('analyzing...');
this.data = '###data###';
}
function LinkDetector(links) {
if (!this instanceof LinkDetector) {
throw new Error('Do not invoke without new.');
}
this.links = links;
DetectorBase.apply(this, arguments);
}
function ContainerDetetor(containers) {
if (!this instanceof ContainerDetetor) {
throw new Error('Do not invoke without new.');
}
this.containers = containers;
DetectorBase.apply(this, arguments);
}
//inherit first
inherit(LinkDetector, DetectorBase);
inherit(ContainerDetetor, DetectorBase);
//expand later
LinkDetector.prototype.dectect = function() {
console.log('Loading data:' + this.data);
console.log('Link detection started');
console.log('Scanding links:' + this.links);
}
ContainerDetetor.prototype.dectect = function() {
console.log('Loading data:' + this.data);
console.log('containers detection started');
console.log('Scanding containers:' + this.containers);
}
//prevent from being altered
Object.freeze(DetectorBase);
Object.freeze(DetectorBase.prototype);
Object.freeze(LinkDetector);
Object.freeze(LinkDetector.prototype);
Object.freeze(ContainerDetetor);
Object.freeze(ContainerDetetor.prototype);
//export to global object
Object.defineProperties(global, {
LinkDetector:{value: LinkDetector},
ContainerDetetor:{value: ContainerDetetor},
DetectorBase:{value: DetectorBase}
});
function inherit(subClass, superClass) {
superClass.prototype = Object.create(superClass.prototype);
superClass.prototype.constructor = subClass;
}
}(this);
var cd = new ContainerDetetor('#abc #def #ghi');
var ld = new LinkDetector('https://www.taobao.com/ https://www.tmall.com/ https://www.baidu.com/');
cd.dectect();
ld.dectect();


提问者:慕粉1927057669 2019-12-13 18:30

个回答

  • 慕田峪2351594
    2020-06-04 11:26:07

    function inherit(subClass, superClass) {

                superClass.prototype = Object.create(superClass.prototype);

                superClass.prototype.constructor = subClass;

            }

    写错了  应该为

    subClass.prototype = Object.create(superClass.prototype);

    subClass.prototype.constructor = subClass;

    注意形参