Javascript基于OOP實(shí)實(shí)現(xiàn)探測器功能代碼實(shí)例
更新時(shí)間:2020年08月26日 11:41:40 作者:starof
這篇文章主要介紹了Javascript基于OOP實(shí)實(shí)現(xiàn)探測器功能代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
代碼如下
<script>
/*所有探測器都有探測的方法和分析的方法,分析當(dāng)前的瀏覽器環(huán)境,不管是瀏覽器還是nodejs*/
/*container容器探測器*/
/*link鏈接探測器*/
/*外層用一個(gè)立即執(zhí)行的匿名函數(shù)包裹住,防止一些函數(shù)聲明或者變量泄露到外面*/
!function(global){
function DetectorBase(configs){//不讓外部通過直接調(diào)用方式調(diào)用,必須使用new,不使用new就會報(bào)錯
/*使用new的話,this就是最后要返回的對象,this instanceof DetectorBase應(yīng)該返回true,不是的話說明沒有直接通過new調(diào)用*/
if(!this instanceof DetectorBase){/**/
throw new Error('Do not invoke without new.');
}
this.configs=configs;/*所有的探測器都會有config屬性*/
this.analyze();/*所有的探測器初始化的時(shí)候都需要解析一下數(shù)據(jù)*/
}
DetectorBase.prototype.detect=function(){/*代表一個(gè)抽象的探測方法,基類不是具體的一個(gè)探測器所以實(shí)現(xiàn)沒有意義,用來說明需要實(shí)現(xiàn)這樣一個(gè)方法*/
throw new Error('Not implemented');
}
DetectorBase.prototype.analyze=function(){
console.log('analyzing...');
this.data="###data###";
}
/***具體實(shí)例***/
function LinkDetector(links){/*鏈接探測器,同理必須通過new來構(gòu)造*/
DetectorBase.apply(this,arguments);
if(!this instanceof LinkDetector){
throw new Error('Do not invoke without new.');
}
this.links=links;
}
function ContainerDetector(containers){
DetectorBase.apply(this,arguments);
if(!this instanceof ContainerDetector){
throw new Error('Do not invoke without new.');
}
this.containers=containers;
}
//inherit first
/*LinkDetector和ContainerDetector都可能掛載一些自己的方法
需要注意,一定要先實(shí)現(xiàn)原型鏈的繼承,再去擴(kuò)展。
因?yàn)槔^承的時(shí)候要改寫LinkDetector的prototype屬性*/
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.containers);
}
//prevent from being altered
/*不希望監(jiān)控程序被改寫,不可刪,不可擴(kuò)展,不可寫*/
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
/*通過defineProperties一次性把3個(gè)類暴露在外面,同時(shí)保護(hù)它們不可被枚舉,不可被刪除和改寫*/
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 ld=new LinkDetector('http://www.taobao.com http://www.tmall.com http://www.baidu.com');
cd.detect();
ld.detect();
</script>
運(yùn)行結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用JS組件實(shí)現(xiàn)帶ToolTip驗(yàn)證框的實(shí)例代碼
這篇文章主要介紹了使用JS組件實(shí)現(xiàn)帶ToolTip驗(yàn)證框的實(shí)例代碼,需要的朋友可以參考下2017-08-08
JavaScript+Canvas實(shí)現(xiàn)帶跳動效果的粒子動畫
這篇文章主要為大家詳細(xì)介紹了如何通過JavaScript和Canvas實(shí)現(xiàn)帶跳動效果的粒子動畫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2023-03-03
JS實(shí)現(xiàn)簡單的頂部定時(shí)關(guān)閉層效果
這篇文章主要介紹了通過JS實(shí)現(xiàn)的簡單頂部定時(shí)關(guān)閉層效果,需要的朋友可以參考下2014-06-06
遍歷json 對象的屬性并且動態(tài)添加屬性的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄闅vjson 對象的屬性并且動態(tài)添加屬性的實(shí)現(xiàn)。小編覺的挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
uniapp改變底部安全區(qū)頂部手機(jī)信號時(shí)間電池欄顏色樣式
這篇文章主要為大家介紹了uniapp改變底部安全區(qū)頂部手機(jī)信號時(shí)間電池欄顏色樣式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
JavaScript forEach()遍歷函數(shù)使用及介紹
這篇文章主要介紹了JavaScript forEach()遍歷函數(shù)使用及介紹,本文講解了使用forEach遍歷數(shù)組的用法以及提前終止循環(huán)的一個(gè)方法技巧,需要的朋友可以參考下2015-07-07

