首頁 web前端 js教程 解析jquery中的核心功能實例

解析jquery中的核心功能實例

Jun 17, 2017 pm 05:49 PM
jquery 深度 解析

核心功能包括:

jquery是如何定義的,如何呼叫的,如何擴充的。掌握核心方法是如何實現的,是理解jQuery原始碼的關鍵。這裡理解了一切豁然開朗。

1,如何定義,即入口

// Define a local copy of jQuery

var jQuery = function( selector, context ) {

    // The jQuery object is actually just the init constructor 'enhanced'

    return new jQuery.fn.init( selector, context, rootjQuery );//jQuery.fn.init( selector, context, rootjQuery );//jQuery.函數jQuery.prototype.init加強版
}

2,jQuery的原型,及與jQuery.fn.init的關係

#//定義物​​件方法,也即只有透過$(“xx”).的方式才能呼叫。

jQuery.fn = jQuery.prototype = {

    init:function( selector, context, rootjQuery ) {

        return jQuery.m

##        return jQuery.m

## ector,

#    }

    其他仍有許多屬性與方法

    屬性有:jquery,constructor, selector, length

  #  方法有: toArray,get, pushStack,each, ready,slice, first,last,eq, map,end, push, sort, splice

}

//把jQuery.prototype賦給jQuery.prototype.init.prototype,是為了後面的實例化

// Give the init function the jQuery prototype for later instantiation

#jQuery.fn.init.prototype = jQuery.fn;

也即是,$(“xx”)擁有了實例方法,可以呼叫。 (呼叫jQuery.prototype下定義的方法)

為什麼jQuery要回傳jQuery.fn.init物件?

jQuery = function( selector, context ) {

#// The jQuery object is actually just the init constructor 'enhanced'

return new jQuery.fn.init( selector, context, rootjQuery );

#}

jQuery.fn = jQuery.prototype = {

    ……

}

jQuery.fn.init.prototype = jQuery.fn;

在stackoverflow 上找到類似問題:

http://stackoverflow.com/questions/4754560/help-understanding-jquerys-jquery-fn-init-why-is-init-in-fn

#還有這個

http://stackoverflow.com/questions/1856890/why-does-jquery-use-new-jquery-fn-init-for-creating-jquery-object-but-i-can/1858537#1858537

I believe the code is written in this fashion so that the new keyword is not required each time you instantiate a new jQuery object and also to delegate the logic behind the object construction to the prototype The for delegate the logic behind the object construction to the prototype The former to make the library cleaner to use and the latter to keep the initialisation logic cleanly in one place and allow init to be recursively called to construct and return an object that correctly matches the passed arguments and return an object that correctly matches the passed arguments.##3,## extend擴充物件方法與靜態方法原理

jQuery.extend = jQuery.fn.extend = function() {

    var target = arguments[0] || {};

    return target;

}

使用extend就方便了,無非就是$.extend({});和$.fn.extend({});如果你能在看到fn時理解聯想到是jQuery.prototype就好了。

再透過this作用域看一下:

$.extend ->this是$-> this.aa()

$.fn.extend-> ;this是$.fn-> this.aa()

附extend實作細節:

使用場景:

1,擴充一些函數

只有一個參數。例如:$.extend({f1:function(){},f2:function(){},f3:function(){}})

2,合併多個物件到第一個物件

(1)淺copy,第一個參數是目標物件。例如

var a = {name:”hello”}

var b = {age:30}

$.extend(a,b);//a= {name:”hello”,age:30}

(2)深copy,第一個參數是TRUE,第二個參數是目標物件。例如

var a = {name:{job:”it”}};

var b = {name:{age: 30 }};


//$ .extend(a,b);

$.extend(true,a,b);

console.log(a);

jQuery.extend = jQuery.fn.extend = function() {
    var options, name, src, copy, copyIsArray, clone,
        target = arguments[0] || {},
        i = 1,
        length = arguments.length,
        deep = false;

    // 是不是深复制  Handle a deep copy situation
    if ( typeof target === "boolean" ) {
        deep = target;
        target = arguments[1] || {};
        // skip the boolean and the target
        i = 2;
    }

    // 不是对象类型  Handle case when target is a string or something (possible in deep copy)
    if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
        target = {};
    }

    // 扩展插件的情况  extend jQuery itself if only one argument is passed
    if ( length === i ) {//$.extend({f1:function(){},f2:function(){},f3:function(){}})
        target = this;//this是$,或是$.fn
        --i;
    }

    for ( ; i < length; i++ ) {//可能有多个对象扩展到第一个对象上
        // Only deal with non-null/undefined values
        if ( (options = arguments[ i ]) != null ) {//options是一个对象
            // Extend the base object
            for ( name in options ) {
                src = target[ name ];  //src是target里已经存在的value(也可能不存在)
                copy = options[ name ];//copy是待合入的一个value

                // 防止循环引用  Prevent never-ending loop
                if ( target === copy ) {//例如:var a={};$.extend(a,{name:a});//可能导致循环引用
                    continue;
                }

                // if是深复制else是浅复制  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;//target[ name ] = options[ name ];
                }
            }
        }
    }

    // Return the modified object
    return target;
};
登入後複製

jQuery. extend({…})分析
看一下如何寫的


jQuery.extend({

prop:""

method:function( ){}

});

可以看出,這些方法是jQuery的靜態屬性和方法(也即是工具方法),將來既可以直接提供給使用者使用,也可以在內部使用。

具體實作的工具屬性和方法有(同時也標註了哪些在內部使用)

jQuery.extend({

        expando  :  產生唯一JQ字串(內部)

        noConflic:t()

       DOM是否已載入(內部)

        readyWait  :  等待檔案的計數器(內部)

        holdRead

        isFunction()  :  是否為函數

        isArray()  :  是否為陣列      isNumeric()  :  是否為數字

        type()  :  判斷
資料型別


        isPlainObject()  :  是否為物件自變數為自變數或符號為「加總

        error()  :  拋出例外

        parseHTML()  :  
      parseXML ()  :  解析XML

        noop()  :  空函數
        globalEval(         nodeName( )  :  是否為指定節點名(內部)

        each()  :  遍歷集合

       真數組

        inArray()  :  陣列版indexOf

        merge()  :  合併陣列     map()  :  映射新陣列

        guid  :  唯一識別碼(內部)

        proxy()  :  調整至 #        now()  :  目前時間

        swap()  :  CSS交換(內部)

}); DOM的非同步運算(內部)

function isArraylike(){}  類似陣列的判斷(內部)

以上是解析jquery中的核心功能實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Oracle錯誤3114詳解:如何快速解決 Oracle錯誤3114詳解:如何快速解決 Mar 08, 2024 pm 02:42 PM

Oracle錯誤3114詳解:如何快速解決,需要具體程式碼範例在Oracle資料庫開發與管理過程中,我們常常會遇到各種各樣的錯誤,其中錯誤3114是比較常見的一個問題。錯誤3114通常表示資料庫連線出現問題,可能是網路故障、資料庫服務停止、或連接字串設定不正確等原因導致的。本文將詳細解釋錯誤3114的產生原因,以及如何快速解決這個問題,並附上具體的程式碼

解析Wormhole NTT:適用於任何Token的開放框架 解析Wormhole NTT:適用於任何Token的開放框架 Mar 05, 2024 pm 12:46 PM

Wormhole在區塊鏈互通性方面處於領先地位,專注於創建有彈性、面向未來的去中心化系統,優先考慮所有權、控制權和無需許可的創新。這個願景的基礎是對技術專業知識、道德原則和社群一致性的承諾,旨在以簡單、清晰和廣泛的多鏈解決方案套件重新定義互通性格局。隨著零知識證明、擴容方案和功能豐富的Token標準的興起,區塊鏈變得更加強大,而互通性也變得越來越重要。在這個不斷創新的應用程式環境中,新穎的治理系統和實用功能為整個網路的資產帶來了前所未有的機會。協議建構者現在正在努力思考如何在這個新興的多鏈

PHP 中點的意思和用法解析 PHP 中點的意思和用法解析 Mar 27, 2024 pm 08:57 PM

【PHP中點的意義和用法解析】在PHP中,中點(.)是常用的運算符,用來連接兩個字串或物件的屬性或方法。在本文中,我們將深入探討PHP中點的意義和用法,並透過具體的程式碼範例加以說明。 1.連接字串中點運算子.在PHP中最常見的用法是連接兩個字串。透過將.放置在兩個字串之間,可以將它們拼接在一起,形成一個新的字串。 $string1=&qu

jQuery小技巧:快速修改頁面所有a標籤的文本 jQuery小技巧:快速修改頁面所有a標籤的文本 Feb 28, 2024 pm 09:06 PM

標題:jQuery小技巧:快速修改頁面所有a標籤的文字在網頁開發中,我們經常需要對頁面中的元素進行修改和操作。使用jQuery時,有時候需要一次修改頁面中所有a標籤的文字內容,這樣可以節省時間和精力。以下將介紹如何使用jQuery快速修改頁面所有a標籤的文本,同時給出具體的程式碼範例。首先,我們需要引入jQuery庫文件,確保在頁面中引入了以下程式碼:&lt

使用jQuery修改所有a標籤的文字內容 使用jQuery修改所有a標籤的文字內容 Feb 28, 2024 pm 05:42 PM

標題:使用jQuery修改所有a標籤的文字內容jQuery是一款受歡迎的JavaScript庫,被廣泛用於處理DOM操作。在網頁開發中,經常會遇到需要修改頁面上連結標籤(a標籤)的文字內容的需求。本文將介紹如何使用jQuery來實現這個目標,並提供具體的程式碼範例。首先,我們需要在頁面中引入jQuery庫。在HTML檔案中加入以下程式碼:

Win11新功能解析:跳過登入微軟帳號的方法 Win11新功能解析:跳過登入微軟帳號的方法 Mar 27, 2024 pm 05:24 PM

Win11新功能解析:跳過登入微軟帳號的方法隨著Windows11的發布,許多用戶發現其帶來了更多的便利性和新功能。然而,有些用戶可能不喜歡將其係統與微軟帳戶綁定,希望跳過這一步驟。本文將介紹一些方法,幫助使用者在Windows11中跳過登入微軟帳戶,並實現更私密、更自主的使用體驗。首先,讓我們來了解為什麼有些用戶不願意登入微軟帳號。一方面,一些用戶擔心他們

Apache2無法正確解析PHP檔案的處理方法 Apache2無法正確解析PHP檔案的處理方法 Mar 08, 2024 am 11:09 AM

由於篇幅限制,以下是一個簡短的文章:Apache2是常用的Web伺服器軟體,而PHP是廣泛使用的伺服器端腳本語言。在建置網站過程中,有時會遇到Apache2無法正確解析PHP檔案的問題,導致PHP程式碼無法執行。這種問題通常是因為Apache2沒有正確配置PHP模組,或是PHP模組與Apache2的版本不相容所導致的。解決這個問題的方法一般有兩種,一種是

如何判斷jQuery元素是否具有特定屬性? 如何判斷jQuery元素是否具有特定屬性? Feb 29, 2024 am 09:03 AM

如何判斷jQuery元素是否具有特定屬性?在使用jQuery操作DOM元素時,常會遇到需要判斷元素是否具有某個特定屬性的情況。在這種情況下,我們可以藉助jQuery提供的方法來輕鬆實現這項功能。以下將介紹兩種常用的方法來判斷一個jQuery元素是否具有特定屬性,並附上具體的程式碼範例。方法一:使用attr()方法和typeof運算子//判斷元素是否具有特定屬

See all articles