欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery源碼解讀之extend()與工具方法、實(shí)例方法詳解

 更新時(shí)間:2017年03月30日 11:16:27   作者:柒青衿  
這篇文章主要介紹了jQuery源碼解讀之extend()與工具方法、實(shí)例方法,分析了jQuery中extend()源碼、功能與相關(guān)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了jQuery源碼解讀之extend()與工具方法、實(shí)例方法。分享給大家供大家參考,具體如下:

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

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

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

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

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

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

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

function A(){
}
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

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

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

function A(){
    this.fun_f=function(){
        console.log("Iam in the constructor");
    };
}
A.prototype.fun_p=function(){
    console.log("Iam in the prototype");
};
var a=new A();
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)先級(jí)為:直接定義在實(shí)例上的變量的優(yōu)先級(jí)要高于定義在“this”上的,而定義在“this”上的又高于 prototype定義的變量。即直接定義在實(shí)例上的變量會(huì)覆蓋定義在“this”上和prototype定義的變量,定義在“this”上的會(huì)覆蓋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)使用的同一個(gè)函數(shù)。

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

(3)當(dāng)extend()中有多個(gè)參數(shù)時(shí),后面的參數(shù)都擴(kuò)展到第一個(gè)參數(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中一個(gè)屬性為對(duì)象:

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}

由于淺拷貝無(wú)法完成,則b.name會(huì)受到a的影響,這時(shí)我們往往希望深拷貝。

深拷貝:

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沒(méi)有覆蓋a.name.job。

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery切換特效與技巧總結(jié)》、《jQuery遍歷算法與技巧總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫(huà)與特效用法總結(jié)》及《jquery選擇器用法總結(jié)

希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • jquery easyui中treegrid用法的簡(jiǎn)單實(shí)例

    jquery easyui中treegrid用法的簡(jiǎn)單實(shí)例

    本篇文章主要是對(duì)jquery easyui中treegrid用法的簡(jiǎn)單實(shí)例進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2014-02-02
  • Jquery使用AJAX方法請(qǐng)求數(shù)據(jù)

    Jquery使用AJAX方法請(qǐng)求數(shù)據(jù)

    本文詳細(xì)講解了Jquery使用AJAX方法請(qǐng)求數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • ajax異步請(qǐng)求詳解

    ajax異步請(qǐng)求詳解

    做前端開(kāi)發(fā)的朋友對(duì)于ajax異步更新一定印象深刻,本文主要介紹了關(guān)于ajax異步請(qǐng)求的那點(diǎn)事,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-01-01
  • Jquery實(shí)現(xiàn)圖片放大鏡效果的思路及代碼(自寫(xiě))

    Jquery實(shí)現(xiàn)圖片放大鏡效果的思路及代碼(自寫(xiě))

    放大鏡類(lèi)的文章網(wǎng)上有很多,由于實(shí)現(xiàn)起來(lái)比較麻煩,所以自己寫(xiě)了一個(gè),下面為大家分享下具體的算法及實(shí)現(xiàn)代碼,感興趣的朋友可以參考下
    2013-10-10
  • jquery實(shí)現(xiàn)div拖拽寬度示例代碼

    jquery實(shí)現(xiàn)div拖拽寬度示例代碼

    本例為大家演示個(gè)比較簡(jiǎn)單的div拖動(dòng),另外可根據(jù)自己的需求,添加相應(yīng)的代碼,實(shí)現(xiàn)自己的想要的效果,具體如下,喜歡的請(qǐng)支持下
    2013-07-07
  • 基于jQuery.Hz2Py.js插件實(shí)現(xiàn)的漢字轉(zhuǎn)拼音特效

    基于jQuery.Hz2Py.js插件實(shí)現(xiàn)的漢字轉(zhuǎn)拼音特效

    jQuery.Hz2Py.js插件實(shí)現(xiàn)的漢字轉(zhuǎn)拼音是一款很實(shí)用的在線(xiàn)轉(zhuǎn)換功能,此插件已經(jīng)把漢字打包成一個(gè)插件庫(kù),調(diào)用的時(shí)間很簡(jiǎn)單,只調(diào)用一個(gè)方法就可以實(shí)現(xiàn)轉(zhuǎn)換了
    2015-05-05
  • jQuery EasyUI API 中文文檔 - PropertyGrid屬性表格

    jQuery EasyUI API 中文文檔 - PropertyGrid屬性表格

    jQuery EasyUI API 中文文檔 - PropertyGrid屬性表格使用介紹,需要的朋友可以參考下。
    2011-11-11
  • 解析jQuery與其它js(Prototype)庫(kù)兼容共存

    解析jQuery與其它js(Prototype)庫(kù)兼容共存

    解決jQuery與其它js(Prototype)庫(kù)沖突的方法很簡(jiǎn)單,就是使用jQuery的jQuery.noConflict()函數(shù),以下小編就為大家介紹,需要的朋友可以參考下
    2013-07-07
  • javascript中對(duì)Attr(dom中屬性)的操作示例講解

    javascript中對(duì)Attr(dom中屬性)的操作示例講解

    這篇文章主要是對(duì)javascript中對(duì)Attr(dom中屬性)的操作進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2013-12-12
  • JQuery的AJAX實(shí)現(xiàn)文件下載的小例子

    JQuery的AJAX實(shí)現(xiàn)文件下載的小例子

    JQuery的ajax函數(shù)的返回類(lèi)型只有xml、text、json、html等類(lèi)型,沒(méi)有“流”類(lèi)型,所以我們要實(shí)現(xiàn)ajax下載,不能夠使用相應(yīng)的ajax函數(shù)進(jìn)行文件下載。但可以用js生成一個(gè)form,用這個(gè)form提交參數(shù),并返回“流”類(lèi)型的數(shù)據(jù)。在實(shí)現(xiàn)過(guò)程中,頁(yè)面也沒(méi)有進(jìn)行刷新
    2013-05-05

最新評(píng)論