浅析 JavaScript 链式调用

前端开发 作者: 2024-08-25 20:35:01
对$函数你已经很熟悉了。它通常返回一个html元素或一个html元素的集合,如下: 但是,如果把这个函数改造为一个构造器,把那些元素作为数组保存在一个实例属性中,并让所有定义在构造器函数的protot
function$(){
    var elements = [];
    for(vari=0,len=arguments.length;i<len;++i){
          var element = arguments[i];
          if(typeof element ===”string”){
              element = document.getElementById(element);
          }
          if(arguments.length==1){
               return element;
           }
           elements.push(element);
    }
      elements;
}
((){

    //use private class
       _$(els){
        this.elements = [];
         ){
             els[i];
             ”string”){
                element  = document.getElementById(element);
             }
              this.elements.push(element)
         }
      }
       The public interface remains the same.

        window.$ = (){
             return new _$(arguments);
        }
})();
(use private class

    ..省略之前上面的代码
    }

    _$.prototype = {
        each:(fn){
            for(var i=0,len=this.elements.length;i<len;i++){
                fn.call(this,.elements[i]);
            }
            ;
        },show:(prop,val){
            var that = ;
            this.each((el){
               that.setStyle("display”,”block");
            });
            (type,fn){
           var add = (el){
               if(window.addEventListener){
                  el.addEventListener(type,fn,false);
               }else (window.attachEvent){
                  el.attachEvent("on"+type,fn);
               }
           };

            (el){
               add(el);
            });
            ;
        }
    };

    
    window.$ = (){
         _$(arguments);
    }

})();
window.installHelper = (scope,interface) {
    scope[interface] = () {
       _$(arguments);
    }
  };
installHelper(window,'$');
$('example').show();
 Define a namespace without overwriting it if it already exists.
window.com = window.com || {};
com.example = com.example || {}; 
com.example.util = com.example.util || {};

installHelper(com.example.util,'get');

(function() {
  var get = com.example.util.get;
  get(example').addEvent(click,function(e) {
    get(this).addClass(hello);
  });
})();
 Accessor without function callbacks: returning requested data in accessors.
window.API = window.API || {};
API.prototype = () {
  var name = 'Hello world' Privileged mutator method.
  setName: (newName) {
    name = newName;
    ;
  },1)"> Privileged accessor method.
  getName: () {
     name;
  }
}();

 Implementation code.
var o =  API;
console.log(o.getName());  Displays 'Hello world'.
console.log(o.setName('Meow').getName());  Displays 'Meow'.

 Accessor with function callbacks.
window.API2 = window.API2 || {};
API2.prototype =  Privileged accessor method.
  通过把函数作为参数传入
  getName: (callback) {
    callback.call(;
  }
}();

var o2 =  API2;
o2.getName(console.log).setName('Meow').getName(console.log);
 Displays 'Hello world' and then 'Meow'.
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68588.html