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

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

miragele

2016-04-25 20:49

!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回答

  • AllenW
    2016-04-27 18:46:54
    已采纳
    /**
     * 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();

    mirage...

    非常感谢!

    2016-04-29 10:59:57

    共 1 条回复 >

  • qq_包包世界我专业_0
    2016-09-11 01:00:05

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

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


    qq_Say...

    为什么要在最后加个(this)才行呢? !function(global){ …… }(this);

    2017-03-19 17:54:32

    共 1 条回复 >

  • 一_叶_知_秋
    2016-08-06 12:54:50

    http://img.mukewang.com/57a56c1500015cc108000206.jpg

    你代码里匿名函数的花括号结束地点错啦,应该在这之后才对,要把所有的函数声明都包含在函数体内,这样才能调用定义的DetectorBase,否则会说DetectorBase是undefined的

    http://img.mukewang.com/57a56c910001a28606690122.jpg

    下面这样就可以运行了

    !function(global){ //用函数包裹以防止基类的变量和函数在外部被更改

    function DetectorBase(config)

    {

    if(!this instanceof DetectorBase){

    throw new Error("Do't invoke without new.");

    }

    this.config = config;

    this.analyze();

    }


    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 ("Don't invoke without new.");

    }

    this.links=links;

    DetectorBase.call(this,arguments[1]);

    }


    function ContainerDetector(containers){

    if(!this instanceof ContainerDetector){

    throw new Error ("Don't invoke without new.");

    }

    this.containers=containers;

    DetectorBase.call(this,arguments[1]);

    }


    //inherit obj

    inherit(LinkDetector,DetectorBase);

    inherit(ContainerDetector,DetectorBase);

    function inherit(subClass,superClass){

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

    subClass.prototype.constructor = subClass;

    }


    //expand child class

    LinkDetector.prototype.detect = function(){

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

    console.log("Link detection started.");

    console.log("Scaning links:"+this.links);

    }


    ContainerDetector.prototype.detect = function(){

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

    console.log("container detection started.");

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

    }


    //prevent from being altered

    Object.freeze(DetectorBase);

    Object.freeze(DetectorBase.prototype);

    Object.freeze(LinkDetector);

    Object.freeze(LinkDetector.prototype);

    Object.freeze(ContainerDetector);

    Object.freeze(ContainerDetector.prototype);

    //export to global object 可以将这些类添加到global对象(对浏览器来说为window对象)同时防止其被更改

    Object.defineProperties(global,{

    LinkDetector:{value:LinkDetector},

    ContainerDetector:{value:ContainerDetector},

    DetectorBase:{value:ContainerDetector}

    });

    }(this);

    var cd = new ContainerDetector("#abc","#def","#ghi");

    var ld = new LinkDetector("http://www.taobao.com");

    cd.detect();

    ld.detect();


JavaScript深入浅出

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

281112 学习 · 1020 问题

查看课程

相似问题