作家
登录

JavaScript的API设计原则

作者: 来源: 2017-06-19 17:57:51 阅读 我要评论

 
  •       dom = [selector], selector = null 
  •  
  •     // If it's a html fragment, create nodes from it 
  •  
  •     else if (fragmentRE.test(selector)) 
  •  
  •       dom = zepto.fragment(selector.trim(), RegExp.$1, context), selector = null 
  •  
  •     // If there's a context, create a collection on that context firstand select 
  •  
  •     // nodes from there 
  •  
  •     else if (context !== undefined) return $(context).find(selector) 
  •  
  •     // And last but no least, if it's a CSS selector, use it to select nodes. 
  •  
  •     else dom = zepto.qsa(document, selector) 
  •  
  •   } 
  •  
  •   // create a new Zepto collection from the nodes found 
  •  
  •   return zepto.Z(dom, selector) 
  •  
  •  
  • 所谓延长就是指函数的应用像流水一样按照书写的次序履行形成履行链条:

    1. document.getElementById('id').style.color = 'red'
    2.  
    3. document.getElementById('id').style.fontSize = '12px'
    4.  
    5. document.getElementById('id').style.backgourdColor = 'pink' 

    用我们之前的之前的办法是再次封装两个函数 setFontSize, setbackgroundColor; 然后履行它们 setColor(‘id’, ‘red’);setFontSiez(‘id’, ’12px’); setbackgroundColor(‘id’, ‘pink’); 显然,如许的做法没有懒出境界来;id元素每次都须要从新获取,影响机能,掉败;每次都须要添加新的办法 掉败 每次还要调用这些办法,照样掉败。下面我们将其改写为可以延长的函数 起首将获取id办法封装查对象,然后再对象的每个办法中返回这个对象:

    1. function getElement(selector) { 
    2.  
    3.   this.style = document.querySelecotrAll(selector).style; 
    4.  
    5.  
    6.   
    7.  
    8. getElement.prototype.color = function(color) { 
    9.  
    10.   this.style.color = color; 

        推荐阅读

        智慧城市发展路径应分级分类

      智能泊车、长途智能抄表、智能路灯杆、智能井盖……聪明城市扶植正在为我们的生活带来各类便利。聪明城市也成为当前热议的话题。6月8日,第二十届中国北京国际科技家当博览会同>>>详细阅读


      本文标题:JavaScript的API设计原则

      地址:http://www.17bianji.com/lsqh/35828.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)