JQuery_2.1.0_日記 5.2

$.方法

(1)$.merge(first, second)
    合併兩個數組或類數組,將第二個數組添加到第一個數組的末尾

(2)$.grep(elems, callback, invert)
    使用callback對elems進行過濾,如果invert設置爲true.則返回保留callback返回值爲false的元素數組,如果invert設置爲false則返回callback返回值爲true的元素數組.

     Test_Script
     var arr = ['a' 'b' 'c' ]
   arr = $.grep(arr, function (){
      //arguments[0]爲elem,arguments[1]爲elem在數組 中的索引
      if (arguments[1] === 1) {
          return false ;
      } else {
         return true ;
      }
   }, true );
   alert(arr[0]); //'b'


(3)$.proxy(fn, context[,args1][,args2]....)
   爲fn綁定上下文執行環境,返回代理後的對象
   Test_Script
   function a() {
       alert(arguments.length) //3
       alert( this .JQuery);  //11
   }
   var proxy = $.proxy(a, {JQuery: '11' }, 'arg1' 'arg2' );
   proxy('args3');
   
   $.proxy源碼
   proxy: function( fn, context ) {
               var tmp, args, proxy;

               //如果參數context爲字符串時修正參數
               if typeof context === "string" ) {
                     tmp = fn[ context ];
                      //執行上下文第一個參數
                     context = fn;
                      //函數爲第一個參數的名爲第二個參數的屬性值
                     fn = tmp;
              }

               // Quick check to determine if target is callable, in the spec
               // this throws a TypeError, but we will just return undefined.
               if ( !jQuery.isFunction( fn ) ) {
                      return undefined ;
              }

               // Simulated bind
               //2個後的參數作爲代理後對象的參數
              args = slice.call( arguments, 2 );
               //代理後的函數
               proxy = function() {
                      return fn.apply( context || this, args.concat( slice.call( arguments ) ) );
              };

               // Set the guid of unique handler to the same of original handler, so it can be removed
               //設置代理函數的唯一標示
               proxy.guid = fn.guid = fn.guid || jQuery.guid++;

               return proxy;
       },
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章