貌似老師的代碼錯了,有哪位大神糾正下
!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
2016-04-27
2016-09-11
我這個也可以的,不用怎么改老師的代碼
!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();
2016-08-06
你代碼里匿名函數(shù)的花括號結(jié)束地點(diǎn)錯啦,應(yīng)該在這之后才對,要把所有的函數(shù)聲明都包含在函數(shù)體內(nèi),這樣才能調(diào)用定義的DetectorBase,否則會說DetectorBase是undefined的
下面這樣就可以運(yùn)行了
!function(global){ //用函數(shù)包裹以防止基類的變量和函數(shù)在外部被更改
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("
cd.detect();
ld.detect();