首頁  >  文章  >  web前端  >  jQuery extend()詳解

jQuery extend()詳解

小云云
小云云原創
2018-01-12 11:18:141235瀏覽

本文主要和大家介紹jQuery extend()詳解及簡單實例的相關資料,需要的朋友可以參考下,希望能幫助到大家。

jQuery extend()詳解及簡單實例

使用jQuery的時候會發現,jQuery中有的函數是這樣使用的:


#
$.get(); 
$.post(); 
$.getJSON();

有些函數是這樣使用的:


#
$('p').css(); 
$('ul').find('li');

有些函數是這樣使用的:


$('li').each(callback); 
$.each(lis,callback);

這裡牽涉到兩個概念:工具方法與實例方法。通常我們說的工具方法是指無需實例化就可以呼叫的函數,如第一段程式碼;實例方法是必須實例化物件以後才可以呼叫的函數,如第二段程式碼。 jQuery中許多方法既是實例方法也是工具方法,只是呼叫方式略有不同,如第三段程式碼。為了更清晰解釋JavaScript中的工具方法與實例方法,進行如下測試。


functionA(){ 
     
  } 
  A.prototype.fun_p=function(){console.log("prototpye");}; 
  A.fun_c=function(){console.log("constructor");}; 
  var a=new A(); 
  A.fun_p();//A.fun_p is not a function 
  A.fun_c();//constructor 
  a.fun_p();//prototpye 
  a.fun_c();//a.fun_c is not a function

透過以上測試可以得出結論,在原型中定義的是實例方法,在建構函式中直接加入的是工具方法;實例方法不能由建構函數調用,同理,工具方法也不能由實例調用。

當然實例方法不只可以在原型中定義,有以下三種定義方法:


#
functionA(){ 
    this.fun_f=function(){ 
        console.log("Iam in the constructor"); 
    }; 
} 
A.prototype.fun_p=function(){ 
    console.log("Iam in the prototype"); 
}; 
var a=newA(); 
a.fun_f();//Iam in the constructor 
a.fun_i=function(){ 
    console.log("Iam in the instance"); 
}; 
a.fun_i();//Iam in the instance 
a.fun_p();//Iam in the prototype

這三種方式的優先權為:直接定義在實例上的變數的優先權要高於定義在「this」上的,而定義在「this」上的又高於prototype定義的變數。即直接定義在實例上的變數會覆寫定義在「this」上和prototype定義的變量,定義在「this」上的會覆寫prototype定義的變數。

下面看jQuery中extend()方法原始碼:


#
jQuery.extend = jQuery.fn.extend = function() { 
    var options,name, src, copy, copyIsArray, clone, 
        target= arguments[0] || {}, 
        i =1, 
        length= arguments.length, 
        deep= false; 
    // Handle adeep copy situation 
    if ( typeoftarget === "boolean" ) { 
        deep= target; 
        //Skip the boolean and the target 
        target= arguments[ i ] || {}; 
        i++; 
    } 
    // Handlecase when target is a string or something (possible in deep copy) 
    if ( typeoftarget !== "object" && !jQuery.isFunction(target) ) { 
        target= {}; 
    } 
    // ExtendjQuery itself if only one argument is passed 
    if ( i ===length ) { 
        target= this; 
        i--; 
    } 
    for ( ; i< length; i++ ) { 
        //Only deal with non-null/undefined values 
        if ((options = arguments[ i ]) != null ) { 
            //Extend the base object 
            for( name in options ) { 
                src= target[ name ]; 
                copy= options[ name ]; 
                //Prevent never-ending loop 
                if( target === copy ) { 
                   continue; 
                } 
                //Recurse if we&#39;re merging plain objects or arrays 
                if( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray= jQuery.isArray(copy)) ) ) { 
                   if( copyIsArray ) { 
                       copyIsArray= false; 
                       clone= src && jQuery.isArray(src) ? src : []; 
                   }else { 
                       clone= src && jQuery.isPlainObject(src) ? src : {}; 
                   } 
                   //Never move original objects, clone them 
                   target[name ] = jQuery.extend( deep, clone, copy ); 
                //Don&#39;t bring in undefined values 
                }else if ( copy !== undefined ) { 
                   target[name ] = copy; 
                } 
            } 
        } 
    } 
    // Returnthe modified object 
    return target; 
};

(1)首先,jQuery和其原型中extend( )方法的實作使用的同一個函數。

(2)當extend()中只有一個參數的時候,是為jQuery物件新增插件。在jQuery上擴展的叫做工具方法,在jQuery.fn(jQuery原型)中擴展的是實例方法,即使在jQuery和原型上擴展相同名字的函數也可以,使用jQuery物件會呼叫工具方法,使用jQuery()會呼叫實例方法。

(3)當extend()中有多個參數時,後面的參數都會擴展到第一個參數。


var a={}; 
$.extend(a,{name:"hello"},{age:10}); 
console.log(a);//Object{name: "hello", age: 10}

(4)淺拷貝(預設):   


var a={}; 
varb={name:"hello"}; 
$.extend(a,b); 
console.log(a);//Object{name: "hello"} 
a.name="hi"; 
console.log(b);//Object{name: "hello"}

b不受a影響,但是如果b中一個屬性為物件:


var a={}; 
varb={name:{age:10}}; 
$.extend(a,b); 
console.log(a.name);//Object{age: 10} 
a.name.age=20; 
console.log(b.name);//Object{age: 20}

由於淺拷貝無法完成,則b.name會受到a的影響,這時我們往往希望深拷貝。

深拷貝:   


#
var a={}; 
varb={name:{age:10}}; 
$.extend(true,a,b); 
console.log(a.name);//Object{age: 10} 
a.name.age=20; 
console.log(b.name);//Object{age: 10}

b.name不受a的影響。


 var a={name:{job:"Web Develop"}}; 
var b={name:{age:10}}; 
$.extend(true,a,b); 
console.log(a.name);//age: 10 job: "Web Develop"


  //b.name没有覆盖a.name.job。

相關推薦:

##jQuery中關於extend()的使用總結

對jQuery中clone()和 extend()的比較與使用

jQuery中$.extend( )用法實例_jquery

以上是jQuery extend()詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn