搜尋
首頁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 <br>jQuery. extend({…})分析<br>看一下如何寫的<p><br>jQuery.extend({<br></p>prop:""<p></p>method:function( ){}<p></p>});<p></p>可以看出,這些方法是jQuery的靜態屬性和方法(也即是工具方法),將來既可以直接提供給使用者使用,也可以在內部使用。 <p></p>具體實作的工具屬性和方法有(同時也標註了哪些在內部使用)<p></p><p>jQuery.extend({<br><br>        expando  :  產生唯一JQ<a href="http://www.php.cn/wiki/57.html" target="_blank">字串</a>(內部)<br><br>        noConflic:t()<br><br>       DOM是否已載入(內部)<br><br>        readyWait  :  等待檔案的計數器(內部)<br><br>        holdRead <br><br>        isFunction()  :  是否為函數<br><br>        isArray()  :  是否為陣列      isNumeric()  :  是否為數字<br><br>        type()  :  判斷<br>資料型別<br><br><br>        isPlainObject()  :  是否為物件自變數為自變數或符號為「加總<br><br>        error()  :  <a href="http://www.php.cn/code/5808.html" target="_blank">拋出例外 </a><br><br>        parseHTML()  :  <br>      parseXML ()  :  解析XML<br><br>        noop()  :  空函數<br><a href="http://www.php.cn/php/php-tp-throw.html" target="_blank">        globalEval( </a>        nodeName( )  :  是否為指定節點名(內部)<br><br>        each()  :  遍歷集合<br><br>       真數組<br><br>        inArray()  :  陣列版indexOf<br><br>        merge()  :  合併陣列     map()  :  映射新陣列<br><br>        guid  :  唯一識別碼(內部)<br><br>        proxy()  :  調整至 #        now()  :  目前時間<br><br>        swap()  :  CSS交換(內部)<br><br>}); DOM的非同步運算(內部)<br><br>function isArraylike(){}  類似陣列的判斷(內部)<br></p>

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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
C和JavaScript:連接解釋C和JavaScript:連接解釋Apr 23, 2025 am 12:07 AM

C 和JavaScript通過WebAssembly實現互操作性。 1)C 代碼編譯成WebAssembly模塊,引入到JavaScript環境中,增強計算能力。 2)在遊戲開發中,C 處理物理引擎和圖形渲染,JavaScript負責遊戲邏輯和用戶界面。

從網站到應用程序:JavaScript的不同應用從網站到應用程序:JavaScript的不同應用Apr 22, 2025 am 12:02 AM

JavaScript在網站、移動應用、桌面應用和服務器端編程中均有廣泛應用。 1)在網站開發中,JavaScript與HTML、CSS一起操作DOM,實現動態效果,並支持如jQuery、React等框架。 2)通過ReactNative和Ionic,JavaScript用於開發跨平台移動應用。 3)Electron框架使JavaScript能構建桌面應用。 4)Node.js讓JavaScript在服務器端運行,支持高並發請求。

Python vs. JavaScript:比較用例和應用程序Python vs. JavaScript:比較用例和應用程序Apr 21, 2025 am 12:01 AM

Python更適合數據科學和自動化,JavaScript更適合前端和全棧開發。 1.Python在數據科學和機器學習中表現出色,使用NumPy、Pandas等庫進行數據處理和建模。 2.Python在自動化和腳本編寫方面簡潔高效。 3.JavaScript在前端開發中不可或缺,用於構建動態網頁和單頁面應用。 4.JavaScript通過Node.js在後端開發中發揮作用,支持全棧開發。

C/C在JavaScript口譯員和編譯器中的作用C/C在JavaScript口譯員和編譯器中的作用Apr 20, 2025 am 12:01 AM

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

JavaScript在行動中:現實世界中的示例和項目JavaScript在行動中:現實世界中的示例和項目Apr 19, 2025 am 12:13 AM

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

JavaScript和Web:核心功能和用例JavaScript和Web:核心功能和用例Apr 18, 2025 am 12:19 AM

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

了解JavaScript引擎:實施詳細信息了解JavaScript引擎:實施詳細信息Apr 17, 2025 am 12:05 AM

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。

Python vs. JavaScript:學習曲線和易用性Python vs. JavaScript:學習曲線和易用性Apr 16, 2025 am 12:12 AM

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

See all articles

熱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

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

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版