Javascript基于OOP实实现探测器功能代码实例


Posted in Javascript onAugust 26, 2020

代码如下

<script>
/*所有探测器都有探测的方法和分析的方法,分析当前的浏览器环境,不管是浏览器还是nodejs*/
/*container容器探测器*/
/*link链接探测器*/

/*外层用一个立即执行的匿名函数包裹住,防止一些函数声明或者变量泄露到外面*/

!function(global){
  function DetectorBase(configs){//不让外部通过直接调用方式调用,必须使用new,不使用new就会报错
    /*使用new的话,this就是最后要返回的对象,this instanceof DetectorBase应该返回true,不是的话说明没有直接通过new调用*/
    if(!this instanceof DetectorBase){/**/
      throw new Error('Do not invoke without new.');
    }
    this.configs=configs;/*所有的探测器都会有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){/*链接探测器,同理必须通过new来构造*/
    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都可能挂载一些自己的方法
  需要注意,一定要先实现原型链的继承,再去扩展。
  因为继承的时候要改写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
  /*不希望监控程序被改写,不可删,不可扩展,不可写*/
  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个类暴露在外面,同时保护它们不可被枚举,不可被删除和改写*/
  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>

运行结果

Javascript基于OOP实实现探测器功能代码实例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
读jQuery之四(优雅的迭代)
Jun 20 Javascript
jQuery中val()方法用法实例
Dec 25 Javascript
JavaScript实现复制内容到粘贴板代码
Mar 31 Javascript
KnockoutJS 3.X API 第四章之事件event绑定
Oct 10 Javascript
js数组与字符串常用方法总结
Jan 13 Javascript
jquery——九宫格大转盘抽奖实例
Jan 16 Javascript
validationEngine 表单验证插件使用实例代码
Jun 15 Javascript
JS图片延迟加载插件LazyImgv1.0用法分析【附demo源码下载】
Sep 04 Javascript
Vue.js子组件向父组件通信的方法实例代码详解
Dec 10 Javascript
深入理解使用Vue实现Context-Menu的思考与总结
Mar 09 Javascript
微信小程序常用赋值方法小结
Apr 30 Javascript
vue 插槽简介及使用示例
Nov 19 Vue.js
Javascript如何实现扩充基本类型
Aug 26 #Javascript
Javascript var变量删除原理及实现
Aug 26 #Javascript
js实现车辆管理系统
Aug 26 #Javascript
js实现飞机大战小游戏
Aug 26 #Javascript
JS面向对象实现飞机大战
Aug 26 #Javascript
JavaScript Image对象实现原理实例解析
Aug 26 #Javascript
js实现飞机大战游戏
Aug 26 #Javascript
You might like
php上传文件,创建递归目录的实例代码
2013/10/18 PHP
PHP实现伪静态方法汇总
2016/01/13 PHP
Yii框架实现邮箱激活的方法【数字签名】
2016/10/18 PHP
多个Laravel项目如何共用migrations详解
2018/09/25 PHP
php和C#的yield迭代器实现方法对比分析
2019/07/17 PHP
JS 统计时间
2021/03/09 Javascript
myFocus slide3D v1.1.0 使用方法与下载
2011/01/12 Javascript
浅谈关于JavaScript的语言特性分析
2013/04/11 Javascript
JQuery结合CSS操作打印样式的方法
2013/12/24 Javascript
nodejs事件的监听与触发的理解分析
2015/02/12 NodeJs
jquery ajax分页插件的简单实现
2016/01/27 Javascript
在JavaScript中模拟类(class)及类的继承关系
2016/05/20 Javascript
理解AngularJs篇:30分钟快速掌握AngularJs
2016/12/23 Javascript
JS匹配日期和时间的正则表达式示例
2017/05/12 Javascript
vue2.0使用swiper组件实现轮播效果
2017/11/27 Javascript
微信小程序项目实践之九宫格实现及item跳转功能
2018/07/19 Javascript
vue设置导航栏、侧边栏为公共页面的例子
2019/11/01 Javascript
[16:04]DOTA2海涛带你玩炸弹 9月5日更新内容详解
2014/09/05 DOTA
Python cookbook(数据结构与算法)将名称映射到序列元素中的方法
2018/03/22 Python
python人民币小写转大写辅助工具
2018/06/20 Python
Python 字符串与数字输出方法
2018/07/16 Python
python爬虫 基于requests模块的get请求实现详解
2019/08/20 Python
python图片二值化提高识别率代码实例
2019/08/24 Python
使用matlab 判断两个矩阵是否相等的实例
2020/05/11 Python
Python+Appium实现自动化清理微信僵尸好友的方法
2021/02/04 Python
玩转CSS3色彩
2010/01/16 HTML / CSS
巴西儿童时尚购物网站:Dinda
2019/08/14 全球购物
英国钻石公司:British Diamond Company
2020/02/16 全球购物
教师辞职报告范文
2014/01/20 职场文书
幼儿园教学随笔感言
2014/02/23 职场文书
入党自荐书范文
2014/03/09 职场文书
车间统计员岗位职责
2015/04/14 职场文书
经营场所证明范本
2015/06/19 职场文书
利用javaScript处理常用事件详解
2021/04/14 Javascript
python井字棋游戏实现人机对战
2022/04/28 Python
基于Redission的分布式锁实战
2022/08/14 Redis