標籤:jquery
$.方法
(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; },