!function(global){
function DetectorBase(configs){
if(!this instanceof DetectorBase){
throw new Error("Don't invoke without new");
}
this.configs = configs;
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('Do not invoke without new');
}
this.links = links;
DetectorBase.apply(this,arguments);
}
function ContainerDetector(contaiers){
if(!this instanceof ContainerDetector){
throw new Error('Do not invoke without new');
}
this.contaiers = contaiers;
DetectorBase.apply(this,arguments);
}
//inherit first
inherit(LinkDetector,DetectorBase);
inherit(ContainerDetector,DetectorBase);
//expand later
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.links);
};
//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
Object.defineProperties(global,{
LinkDetector:{value:LinkDetector},
ContainerDetector:{value:ContainerDetector},
DetectorBase:{value:DetectorBase}
});
function inherit(subClass,superClass){
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
}
}(this);
var cd = new ContainerDetector('#abc #def #ghi');
var Id = new LinkDetector('http://www.taobao.com http://www.tmall.com http://www.baidu,com');
cd.detect();
Id.detect();
判读一个对象是不是使用new调用:
使用new调用this就是最后要返回对象,使用this instanceof obj 判读 this是不是该对象的实例 如果返回true 说明是使用new进行调用
//函数前面加上!表示立即执行,如!function(global){}(this);传入this值作为形参global; !function(global){ //定义基站的实例属性,用构造函数模式 function DetectorBase(configs){ if(!this instanceof DetectorBase){ throw new Error('Do not invoke without new.'); } this.configs=configs; this.analyze(); } //定义基站的实例共享属性detect和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('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); } //让链接和容器探测器都继承基站的共享属性 inherit(LinkDetector,DetectorBase); inherit(ContainerDetector,DetectorBase); //定义链接和容器探测器的实例共享属性 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); }; //冻结探测器的属性特性,以防被修改 Object.freeze(DetectorBase.prototype); Object.freeze(DetectorBase); Object.freeze(LinkDetector); Object.freeze(LinkDetector.prototype); Object.freeze(ContainerDetector); Object.freeze(ContainerDetector.prototype); //将探测器方法变为global这个传入的全局变量的属性,可以将方法传出到全局环境中 Object.defineProperties(global,{ LinkDetector:{value:LinkDetector}, ContainerDetector:{value:ContainerDetector}, DetectorBase:{value:DetectorBase} }); //继承方法 function inherit(subClass,superClass){ subClass.prototype=Object.create(superClass.prototype); subClass.prototype.constructor=subClass; } }(this); //全局环境下... var cd=new ContainerDetector("#abc #ddfe #fasdfa"); var ld=new LinkDetector("http://www.baidu.com http://www.baidu.com http://www.baidu.com"); cd.detect(); ld.detect(); console.log(cd.data); console.log(cd.containers);