所谓延长就是指函数的应用像流水一样按照书写的次序履行形成履行链条:
- document.getElementById('id').style.color = 'red';
- document.getElementById('id').style.fontSize = '12px';
- document.getElementById('id').style.backgourdColor = 'pink';
用我们之前的之前的办法是再次封装两个函数 setFontSize, setbackgroundColor; 然后履行它们 setColor(‘id’, ‘red’);setFontSiez(‘id’, ’12px’); setbackgroundColor(‘id’, ‘pink’); 显然,如许的做法没有懒出境界来;id元素每次都须要从新获取,影响机能,掉败;每次都须要添加新的办法 掉败 每次还要调用这些办法,照样掉败。下面我们将其改写为可以延长的函数 起首将获取id办法封装查对象,然后再对象的每个办法中返回这个对象:
- function getElement(selector) {
- this.style = document.querySelecotrAll(selector).style;
- }
- getElement.prototype.color = function(color) {
- this.style.color = color;
推荐阅读
智能泊车、长途智能抄表、智能路灯杆、智能井盖……聪明城市扶植正在为我们的生活带来各类便利。聪明城市也成为当前热议的话题。6月8日,第二十届中国北京国际科技家当博览会同>>>详细阅读
本文标题:JavaScript的API设计原则
地址:http://www.17bianji.com/lsqh/35828.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示