加入收藏 | 设为首页 | 会员中心 | 我要投稿 济源站长网 (https://www.0391zz.cn/)- 数据工具、数据仓库、行业智能、CDN、运营!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

如何用不到200行代码写一款属于自己的JS类库

发布时间:2019-07-06 12:32:21 所属栏目:优化 来源:徐小夕
导读:前言 JavaScript 的核心是支持面向对象的,同时它也提供了强大灵活的 OOP 语言能力。本文将使用面向对象的方式,来教大家用原生js写出一个类似jQuery这样的类库。我们将会学到如下知识点: 闭包:减少变量污染,缩短变量查找范围 自执行函数在对象中的运用

前言

JavaScript 的核心是支持面向对象的,同时它也提供了强大灵活的 OOP 语言能力。本文将使用面向对象的方式,来教大家用原生js写出一个类似jQuery这样的类库。我们将会学到如下知识点:

  • 闭包:减少变量污染,缩短变量查找范围
  • 自执行函数在对象中的运用
  • extend的实现原理
  • 如何实现跨浏览器的事件监听
  • 原型链与继承

接下来我会对类库的核心api进行讲解和展示,文章最后后附带类库的完整源码。

类库设计思路

如何用不到200行代码写一款属于自己的JS类库

API介绍和效果展示

事件绑定 Xuery.on(eventName, fn) 案例如下:

  1. Xuery('#demo').on('click', function(e){ 
  2.     alert('hello world!') 
  3. }) 

访问和设置css Xuery.css(string|object, ?[string]) 案例如下:

  1. // 访问css 
  2. Xuery('#demo').css('width') 
  3. // 设置css 
  4. Xuery('#demo').css('width', '1024px') 
  5. // 设置css 
  6. Xuery('#demo').css({ 
  7.     width: '1024px', 
  8.     height: '1024px' 
  9. }) 

访问和设置属性 Xuery.attr(string|object, ?[string]) 案例如下:

  1. // 访问attr 
  2. Xuery('#demo').attr('title') 
  3. // 设置attr 
  4. Xuery('#demo').attr('title', '1024px') 
  5. // 设置attrs 
  6. Xuery('#demo').attr({ 
  7.     title: '1024px', 
  8.     name: '1024px' 
  9. }) 

访问和设置html 案例如下:

  1. // 访问 
  2. Xuery('#demo').html() 
  3. // 设置 
  4. Xuery('#demo').html('前端学习原生框架') 

还有其他几个常用的API在这里就不介绍了,大家可以在我的github上查看,或者基于这套基础框架,去扩展属于自己的js框架。

核心源码

以下源码相关功能我做了注释,建议大家认真阅读,涉及到原型链和构造函数的指向的问题,是实现上述调用方式的核心,又不懂可以在评论区交流沟通。

  1. /** 
  2.  * 链模式实现自己的js类库 
  3.  */ 
  4. (function(win, doc){ 
  5.     var Xuery = function(selector, context) { 
  6.         return new Xuery.fn.init(selector, context) 
  7.     }; 
  8.  
  9.     Xuery.fn = Xuery.prototype = { 
  10.     constructor: Xuery, 
  11.     init: function(selector, context) { 
  12.         // 设置元素长度 
  13.         this.length = 0; 
  14.         // 默认获取元素的上下文document 
  15.         context = context || document; 
  16.         // id选择符,则按位非将-1转化为0 
  17.         if(~selector.indexOf('#')) { 
  18.         this[0] = document.getElementById(selector.slice(1)); 
  19.         this.length = 1; 
  20.         }else{ 
  21.         // 在上下文中选择元素 
  22.         var doms = context.getElementsByTagName(selector), 
  23.         i = 0, 
  24.         len = doms.length; 
  25.         for(; i<len; i++){ 
  26.             this[i] = doms[i]; 
  27.         } 
  28.         } 
  29.         this.context = context; 
  30.         this.selector = selector; 
  31.         return this 
  32.     }, 
  33.     // 增强数组 
  34.     push: [].push, 
  35.     sort: [].sort, 
  36.     splice: [].splice 
  37.     }; 
  38.  
  39.     // 方法扩展 
  40.     Xuery.extend = Xuery.fn.extend = function(){ 
  41.     // 扩展对象从第二个参数算起 
  42.     var i = 1, 
  43.     len = arguments.length, 
  44.     target = arguments[0], 
  45.     j; 
  46.     if(i === len){ 
  47.         target = this; 
  48.         i--; 
  49.     } 
  50.     // 将参数对象合并到target 
  51.     for(; i<len; i++){ 
  52.         for(j in arguments[i]){ 
  53.         target[j] = arguments[i][j]; 
  54.         } 
  55.     } 
  56.     return target 
  57.     } 
  58.  
  59.     // 扩展事件方法 
  60.     Xuery.fn.extend({ 
  61.     on: (function(){ 
  62.         if(document.addEventListener){ 
  63.         return function(type, fn){ 
  64.             var i = this.length -1; 
  65.             for(; i>=0;i--){ 
  66.             this[i].addEventListener(type, fn, false) 
  67.             } 
  68.             return this 
  69.         } 
  70.         // ie浏览器dom2级事件 
  71.         }else if(document.attachEvent){ 
  72.         return function(type, fn){ 
  73.             var i = this.length -1; 
  74.             for(; i>=0;i--){ 
  75.             this[i].addEvent('on'+type, fn) 
  76.             } 
  77.             return this 
  78.         } 
  79.         // 不支持dom2的浏览器 
  80.         }else{ 
  81.         return function(type, fn){ 
  82.             var i = this.length -1; 
  83.             for(; i>=0;i--){ 
  84.             this[i]['on'+type] = fn; 
  85.             } 
  86.             return this 
  87.         } 
  88.         } 
  89.     })() 
  90.     }) 
  91.  
  92.     // 将‘-’分割线转换为驼峰式 
  93.     Xuery.extend({ 
  94.     camelCase: function(str){ 
  95.         return str.replace(/-(w)/g, function(all, letter){ 
  96.         return letter.toUpperCase(); 
  97.         }) 
  98.     } 
  99.     }) 
  100.  
  101.     // 设置css 
  102.     Xuery.extend({ 
  103.     css: function(){ 
  104.         var arg = arguments, 
  105.         len = arg.length; 
  106.         if(this.length < 1){ 
  107.         return this 
  108.         } 
  109.         if(len === 1) { 
  110.         if(typeof arg[0] === 'string') { 
  111.             if(this[0].currentStyle){ 
  112.             return this[0].currentStyle[arg[0]]; 
  113.             }else{ 
  114.             return getComputedStyle(this[0], false)[arg[0]] 
  115.             } 
  116.         }else if(typeof arg[0] === 'object'){ 
  117.             for(var i in arg[0]){ 
  118.             for(var j=this.length -1; j>=0; j--){ 
  119.                 this[j].style[Xuery.camelCase(i)] = arg[0][i]; 
  120.             } 
  121.             } 
  122.         } 
  123.         }else if(len === 2){ 
  124.         for(var j=this.length -1; j>=0; j--){ 
  125.             this[j].style[Xuery.camelCase(arg[0])] = arg[1]; 
  126.         } 
  127.         } 
  128.         return this 
  129.     } 
  130.     }) 
  131.  
  132.     // 设置属性 
  133.     Xuery.extend({ 
  134.     attr: function(){ 
  135.         var arg = arguments, 
  136.         len = arg.length; 
  137.         if(len <1){ 
  138.         return this 
  139.         } 
  140.         if(len === 1){ 
  141.         if(typeof arg[0] === 'string'){ 
  142.             return this[0].getAttribute(arg[0]) 
  143.         }else if(typeof arg[0] === 'object'){ 
  144.             for(var i in arg[0]){ 
  145.             for(var j=this.length -1; j>= 0; j--){ 
  146.                 this[j].setAttribute(i, arg[0][i]) 
  147.             } 
  148.             } 
  149.         } 
  150.         } 
  151.         else if(len === 2){ 
  152.         for(var j=this.length -1; j>=0; j--){ 
  153.             this[j].setAttribute(arg[0], arg[1]); 
  154.         } 
  155.         } 
  156.         return this 
  157.     } 
  158.     }) 
  159.  
  160.     // 获取或者设置元素内容 
  161.     Xuery.fn.extend({ 
  162.     html: function(){ 
  163.         var arg = arguments, 
  164.         len = arg.length; 
  165.         if(len === 0){ 
  166.         return this[0] && this[0].innerHTML 
  167.         }else{ 
  168.         for(var i=this.length -1; i>=0; i--){ 
  169.             this[i].innerHTML = arg[0]; 
  170.         } 
  171.         } 
  172.         return this 
  173.     } 
  174.     }) 
  175.  
  176.     Xuery.fn.init.prototype = Xuery.fn; 
  177.     window.Xuery = Xuery; 
  178. })(window, document); 

(编辑:济源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读