慕粉1927057669
2019-12-13 18:30
//=实战 探测器 !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();
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深入浅出
281101 学习 · 1020 问题
相似问题