亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery extend()詳解及簡單實(shí)例

 更新時間:2017年05月06日 10:44:29   投稿:lqh  
這篇文章主要介紹了jQuery extend()詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下

jQuery extend()詳解及簡單實(shí)例

使用jQuery的時候會發(fā)現(xiàn),jQuery中有的函數(shù)是這樣使用的:

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

有些函數(shù)是這樣使用的:

$('div').css(); 
$('ul').find('li'); 

有些函數(shù)是這樣使用的:

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

這里涉及到兩個概念:工具方法與實(shí)例方法。通常我們說的工具方法是指無需實(shí)例化就可以調(diào)用的函數(shù),如第一段代碼;實(shí)例方法是必須實(shí)例化對象以后才可以調(diào)用的函數(shù),如第二段代碼。jQuery中很多方法既是實(shí)例方法也是工具方法,只是調(diào)用方式略有不同,如第三段代碼。為了更清晰解釋JavaScript中的工具方法與實(shí)例方法,進(jìn)行如下測試。

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 

通過以上測試可以得出結(jié)論,在原型中定義的是實(shí)例方法,在構(gòu)造函數(shù)中直接添加的是工具方法;實(shí)例方法不能由構(gòu)造函數(shù)調(diào)用,同理,工具方法也不能由實(shí)例調(diào)用。

當(dāng)然實(shí)例方法不僅可以在原型中定義,有以下三種定義方法:

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 

這三種方式的優(yōu)先級為:直接定義在實(shí)例上的變量的優(yōu)先級要高于定義在“this”上的,而定義在“this”上的又高于 prototype定義的變量。即直接定義在實(shí)例上的變量會覆蓋定義在“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'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't bring in undefined values 
                }else if ( copy !== undefined ) { 
                   target[name ] = copy; 
                } 
            } 
        } 
    } 
    // Returnthe modified object 
    return target; 
}; 

(1)首先,jQuery和其原型中extend()方法的實(shí)現(xiàn)使用的同一個函數(shù)。

(2)當(dāng)extend()中只有一個參數(shù)的時候,是為jQuery對象添加插件。在jQuery上擴(kuò)展的叫做工具方法,在jQuery.fn(jQuery原型)中擴(kuò)展的是實(shí)例方法,即使在jQuery和原型上擴(kuò)展相同名字的函數(shù)也可以,使用jQuery對象會調(diào)用工具方法,使用jQuery()會調(diào)用實(shí)例方法。

(3)當(dāng)extend()中有多個參數(shù)時,后面的參數(shù)都擴(kuò)展到第一個參數(shù)上。

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

(4)淺拷貝(默認(rèn)):   

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。 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • jQuery插件分享之分頁插件jqPagination

    jQuery插件分享之分頁插件jqPagination

    jqPagination 是一個簡單易用的輕量級 jQuery分頁插件,其使用了 HTML5 和 CSS3 技術(shù)來實(shí)現(xiàn)。此插件提供了幾個參數(shù)設(shè)置選項,通過簡單的配置即可生成分頁控件。此外,它的外觀樣式是可自定義的,擴(kuò)展性很強(qiáng)。
    2014-06-06
  • 基于JQuery的實(shí)現(xiàn)圖片輪播效果(焦點(diǎn)圖)

    基于JQuery的實(shí)現(xiàn)圖片輪播效果(焦點(diǎn)圖)

    用JQuery操作DOM確實(shí)很方便,并且JQuery提供了非常人性化的API應(yīng)付我們的各種需求,其中選擇器在此示例-JQuery實(shí)現(xiàn)圖片輪播效果上體現(xiàn)的尤為出色。大大簡化了js的代碼。
    2010-09-09
  • jquery實(shí)現(xiàn)頂部向右伸縮的導(dǎo)航區(qū)域代碼

    jquery實(shí)現(xiàn)頂部向右伸縮的導(dǎo)航區(qū)域代碼

    這篇文章主要介紹了jquery實(shí)現(xiàn)頂部向右伸縮的導(dǎo)航區(qū)域代碼,涉及jquery鼠標(biāo)click點(diǎn)擊事件及頁面元素動態(tài)操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • jquery實(shí)現(xiàn)多屏多圖焦點(diǎn)圖切換特效的方法

    jquery實(shí)現(xiàn)多屏多圖焦點(diǎn)圖切換特效的方法

    這篇文章主要介紹了jquery實(shí)現(xiàn)多屏多圖焦點(diǎn)圖切換特效的方法,涉及jQuery插件jquery.kinMaxShow實(shí)現(xiàn)焦點(diǎn)圖的相關(guān)技巧,非常具有實(shí)用價值,需要的朋友可以參考下
    2015-05-05
  • 如何理解jQuery中的ajaxSubmit方法

    如何理解jQuery中的ajaxSubmit方法

    本文主要介紹了jQuery中的ajaxSubmit方法的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • jQuery 1.5最新版本的改進(jìn)細(xì)節(jié)分析

    jQuery 1.5最新版本的改進(jìn)細(xì)節(jié)分析

    jQuery 1.5 beta1出來了,從學(xué)習(xí)跟進(jìn)上來說,這一次已經(jīng)比較晚了(我竟然不知道1.5什么時候出的alpha,就這么beta了)。
    2011-01-01
  • jquery實(shí)現(xiàn)圖片放大鏡效果

    jquery實(shí)現(xiàn)圖片放大鏡效果

    這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)圖片放大鏡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • jquery實(shí)現(xiàn)ajax加載超時提示的方法

    jquery實(shí)現(xiàn)ajax加載超時提示的方法

    這篇文章主要介紹了jquery實(shí)現(xiàn)ajax加載超時提示的方法,涉及jQuery中l(wèi)oad方法的ajax加載超時設(shè)置與提示信息處理技巧,需要的朋友可以參考下
    2016-07-07
  • jQuery自制提示框tooltip改進(jìn)版

    jQuery自制提示框tooltip改進(jìn)版

    這篇文章主要介紹了jQuery自制提示框tooltip改進(jìn)版,將tooltip內(nèi)容加上圖片預(yù)覽,感興趣的小伙伴們可以參考一下
    2016-08-08
  • jQuery之動畫ajax事件(實(shí)例講解)

    jQuery之動畫ajax事件(實(shí)例講解)

    下面小編就為大家?guī)硪黄猨Query之動畫ajax事件(實(shí)例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07

最新評論