为了账号安全,请及时绑定邮箱和手机立即绑定

貌似老师的代码错了,有哪位大神纠正下

!function(global){
	function DetectorBase(configs){
if (!this instanceof DetectorBase) {
	throw new Error('Do not invoke without new.')
   }
   this.configs=configs;
   this.analyze();  
	}
}(this);
DetectorBase.prototype.detect = 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 ContainerDetector(containers){
	if (!this instanceof ContainerDetector) {
		throw new Error('Do not invoke without new.')
	}
	this.containers=containers;
	DetectorBase.apply(this,arguments);
}
function inherit(subClass,superClass){
subClass.prototype=Object.create(superClass.prototype)
subClass.prototype.constructor=subClass;
}
inherit(LinkDetector,DetectorBase);
inherit(ContainerDetector,DetectorBase);
LinkDetector.prototype.detect=function(){
	console.log('Loading data:'+this.data);
	console.log('link detection started.');
	console.log('Scaning link:'+this.links);
};
ContainerDetector.prototype.detect=function(){
	console.log('Loading data:'+this.data);
	console.log('Container detection started.');
	console.log('Scaning containers:'+this.containers);
}
Object.defineProperties(global,{
LinkDetector:{value:LinkDetector},
ContainerDetector:{value:ContainerDetector},
DetectorBase:{value:DetectorBase}
});
var a=new ContainerDetector('#abc');
var b=new LinkDetector('http://www.baidu.com')
a.detect;
b.detect;

VM25187:11 Uncaught ReferenceError: DetectorBase is not defined

正在回答

3 回答

/**
 * Created by wz on 16/4/27.
 */
 
 //这样应该可以了
var Detec=(function(global){
    return{
        cons:function DetectorBase(configs){
            if (!this instanceof DetectorBase) {
                throw new Error('Do not invoke without new.')
            }
            this.configs=configs;
            this.analyze();
        }
    }
})(this);
Detec.cons.prototype.analyze=function(){
    console.log('Analyzing...');
    this.data="##data##";
};
Detec.cons.prototype.detect=function () {
    throw new Error('Not implemented');
};
function LinkDetector(links){
    if (!this instanceof LinkDetector) {
        throw new Error('Do not invoke without new.')
    }
    this.links=links;
    Detec.cons.apply(this,arguments);
}
function ContainerDetector(containers){
    if (!this instanceof ContainerDetector) {
        throw new Error('Do not invoke without new.')
    }
    this.containers=containers;
    Detec.cons.apply(this,arguments);
}
function inherit(subClass,superClass){
    subClass.prototype=Object.create(superClass.prototype);
    subClass.prototype.cons=subClass;
}
inherit(LinkDetector,Detec.cons);
inherit(ContainerDetector,Detec.cons);
LinkDetector.prototype.detect=function(){
    //console.log(this);
    //alert(this instanceof LinkDetector);
    console.log('Loading data:'+this.data);
    console.log('link detection started.');
    console.log('Scaning link:'+this.links);
};
ContainerDetector.prototype.detect=function(){
    console.log('Loading data:'+this.data);
    console.log('Container detection started.');
    console.log('Scaning containers:'+this.containers);
};
Object.defineProperties(this,{
    LinkDetector:{value:LinkDetector},
    ContainerDetector:{value:ContainerDetector},
    DetectorBase:{value:Detec.cons}
});
var a=new ContainerDetector('#abc');
var b=new LinkDetector('http://www.baidu.com');

a.detect();
b.detect();
0 回复 有任何疑惑可以回复我~
#1

miragele 提问者

非常感谢!
2016-04-29 回复 有任何疑惑可以回复我~

我这个也可以的,不用怎么改老师的代码

!function(global){

    function DetectorBase(configs){

if (!this instanceof DetectorBase) {

    throw new Error('Do not invoke without new.')

   }

   this.configs=configs;

   this.analyze();  

    }

window["DetectorBase"]=DetectorBase;

}(this);

DetectorBase.prototype.detect = 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 ContainerDetector(containers){

    if (!this instanceof ContainerDetector) {

        throw new Error('Do not invoke without new.')

    }

    this.containers=containers;

    DetectorBase.apply(this,arguments);

}

function inherit(subClass,superClass){

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

subClass.prototype.constructor=subClass;

}

inherit(LinkDetector,DetectorBase);

inherit(ContainerDetector,DetectorBase);

LinkDetector.prototype.detect=function(){

    console.log('Loading data:'+this.data);

    console.log('link detection started.');

    console.log('Scaning link:'+this.links);

};

ContainerDetector.prototype.detect=function(){

    console.log('Loading data:'+this.data);

    console.log('Container detection started.');

    console.log('Scaning containers:'+this.containers);

}

Object.defineProperties(this,{

LinkDetector:{value:LinkDetector},

ContainerDetector:{value:ContainerDetector},

DetectorBase:{value:DetectorBase}

});

var a=new ContainerDetector('#abc');

var b=new LinkDetector('http://www.baidu.com')

a.detect();

b.detect();


1 回复 有任何疑惑可以回复我~
#1

qq_Say_3

为什么要在最后加个(this)才行呢? !function(global){ …… }(this);
2017-03-19 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

貌似老师的代码错了,有哪位大神纠正下

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信