Javascript基于OOP實(shí)實(shí)現(xiàn)探測(cè)器功能代碼實(shí)例
更新時(shí)間:2020年08月26日 11:41:40 作者:starof
這篇文章主要介紹了Javascript基于OOP實(shí)實(shí)現(xiàn)探測(cè)器功能代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
代碼如下
<script> /*所有探測(cè)器都有探測(cè)的方法和分析的方法,分析當(dāng)前的瀏覽器環(huán)境,不管是瀏覽器還是nodejs*/ /*container容器探測(cè)器*/ /*link鏈接探測(cè)器*/ /*外層用一個(gè)立即執(zhí)行的匿名函數(shù)包裹住,防止一些函數(shù)聲明或者變量泄露到外面*/ !function(global){ function DetectorBase(configs){//不讓外部通過(guò)直接調(diào)用方式調(diào)用,必須使用new,不使用new就會(huì)報(bào)錯(cuò) /*使用new的話,this就是最后要返回的對(duì)象,this instanceof DetectorBase應(yīng)該返回true,不是的話說(shuō)明沒(méi)有直接通過(guò)new調(diào)用*/ if(!this instanceof DetectorBase){/**/ throw new Error('Do not invoke without new.'); } this.configs=configs;/*所有的探測(cè)器都會(huì)有config屬性*/ this.analyze();/*所有的探測(cè)器初始化的時(shí)候都需要解析一下數(shù)據(jù)*/ } DetectorBase.prototype.detect=function(){/*代表一個(gè)抽象的探測(cè)方法,基類(lèi)不是具體的一個(gè)探測(cè)器所以實(shí)現(xiàn)沒(méi)有意義,用來(lái)說(shuō)明需要實(shí)現(xiàn)這樣一個(gè)方法*/ throw new Error('Not implemented'); } DetectorBase.prototype.analyze=function(){ console.log('analyzing...'); this.data="###data###"; } /***具體實(shí)例***/ function LinkDetector(links){/*鏈接探測(cè)器,同理必須通過(guò)new來(lái)構(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í)候要改寫(xiě)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)控程序被改寫(xiě),不可刪,不可擴(kuò)展,不可寫(xiě)*/ 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 /*通過(guò)defineProperties一次性把3個(gè)類(lèi)暴露在外面,同時(shí)保護(hù)它們不可被枚舉,不可被刪除和改寫(xiě)*/ 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é)果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
不間斷循環(huán)滾動(dòng)效果的實(shí)例代碼(必看篇)
下面小編就為大家?guī)?lái)一篇不間斷循環(huán)滾動(dòng)效果的實(shí)例代碼(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10extract-text-webpack-plugin用法詳解
這篇文章主要介紹了extract-text-webpack-plugin用法詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02electron-builder 的基本使用及electron打包步驟
electron-builder 作為一個(gè)用于 Electron 應(yīng)用程序打包的工具,需要下載并使用 Electron 運(yùn)行時(shí)來(lái)創(chuàng)建可執(zhí)行文件,這篇文章主要介紹了electron-builder 的基本使用,需要的朋友可以參考下2023-12-12JS如何通過(guò)視頻鏈接獲取視頻時(shí)長(zhǎng)
這篇文章主要介紹了JS如何通過(guò)視頻鏈接獲取視頻時(shí)長(zhǎng),這個(gè)函數(shù)用提供的URL創(chuàng)建一個(gè)新的Video元素,并在loadedmetadata事件被觸發(fā)時(shí)解析一個(gè)帶有視頻持續(xù)時(shí)間的Promise,感興趣的朋友跟隨小編一起看看吧2024-06-06uniapp小程序之配置首頁(yè)搜索框功能的實(shí)現(xiàn)
這篇文章主要介紹了uniapp小程序之配置首頁(yè)搜索框,我們介紹一下本次開(kāi)發(fā)使用的是uniapp,本次分享內(nèi)容的搜索框?yàn)榻馆斎胨阉骺?,點(diǎn)擊跳轉(zhuǎn)專屬搜索頁(yè)面,需要的朋友可以參考下2022-09-09