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

来源:9-2 实践(探测器)

慕粉1927057669

2019-12-13 18:30

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();


写回答 关注

1回答

  • 慕田峪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;

    注意形参


JavaScript深入浅出

由浅入深学习JS语言特性,且解析JS常见误区,从入门到掌握

281101 学习 · 1020 问题

查看课程

相似问题