<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

        深度解析jquery中的核心功能

        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 20:19:39
        文檔

        深度解析jquery中的核心功能

        深度解析jquery中的核心功能:核心功能包括:jquery是如何定義的,如何調(diào)用的,如何擴(kuò)展的。掌握核心方法是如何實現(xiàn)的,是理解jQuery源碼的關(guān)鍵。這里理解了一切豁然開朗。1,如何定義,即入口// Define a local copy of jQueryvar jQuery = function( selector
        推薦度:
        導(dǎo)讀深度解析jquery中的核心功能:核心功能包括:jquery是如何定義的,如何調(diào)用的,如何擴(kuò)展的。掌握核心方法是如何實現(xiàn)的,是理解jQuery源碼的關(guān)鍵。這里理解了一切豁然開朗。1,如何定義,即入口// Define a local copy of jQueryvar jQuery = function( selector

        核心功能包括:

        jquery是如何定義的,如何調(diào)用的,如何擴(kuò)展的。掌握核心方法是如何實現(xiàn)的,是理解jQuery源碼的關(guān)鍵。這里理解了一切豁然開朗。

        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對象僅僅是構(gòu)造函數(shù)jQuery.prototype.init加強版

        }

        2,jQuery的原型,及與jQuery.fn.init的關(guān)系

        //定義對象方法,也即只有通過$(“xx”).的方式才能調(diào)用。

        jQuery.fn = jQuery.prototype = {

        init:function( selector, context, rootjQuery ) {

        return jQuery.makeArray( selector, this );

        }

        其他還有很多屬性和方法,

        屬性有: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”)擁有了實例方法,可以調(diào)用。(調(diào)用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 former I believe is 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.

        3,extend擴(kuò)展對象方法和靜態(tài)方法原理

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

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

        return target;

        }

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

        再通過this作用域看一下:

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

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

        附extend實現(xiàn)細(xì)節(jié):

        使用場景:

        1,擴(kuò)展一些函數(shù)

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

        2,合并多個對象到第一個對象

        (1)淺copy,第一個參數(shù)是目標(biāo)對象。例如

        var a = {name:”hello”}

        var b = {age:30}

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

        (2)深copy,第一個參數(shù)是TRUE,第二個參數(shù)是目標(biāo)對象。例如

        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;
        
         // 是不是深復(fù)制 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 = {};
         }
        
         // 擴(kuò)展插件的情況 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++ ) {//可能有多個對象擴(kuò)展到第一個對象上
         // 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里已經(jīng)存在的value(也可能不存在)
         copy = options[ name ];//copy是待合入的一個value
        
         // 防止循環(huán)引用 Prevent never-ending loop
         if ( target === copy ) {//例如:var a={};$.extend(a,{name:a});//可能導(dǎo)致循環(huán)引用
         continue;
         }
        
         // if是深復(fù)制else是淺復(fù)制 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的靜態(tài)屬性和方法(也即是工具方法),將來既可以直接提供給用戶使用,也可以在內(nèi)部使用。

        具體實現(xiàn)的工具屬性和方法有(同時也標(biāo)注了哪些在內(nèi)部使用)

        jQuery.extend({

        expando : 生成唯一JQ字符串(內(nèi)部)

        noConflict() : 防止沖突

        isReady : DOM是否加載完(內(nèi)部)

        readyWait : 等待多少文件的計數(shù)器(內(nèi)部)

        holdReady() : 推遲DOM觸發(fā)

        ready() : 準(zhǔn)備DOM觸發(fā)

        isFunction() : 是否為函數(shù)

        isArray() : 是否為數(shù)組

        isWindow() : 是否為window

        isNumeric() : 是否為數(shù)字

        type() : 判斷數(shù)據(jù)類型

        isPlainObject() : 是否為對象自變量

        isEmptyObject() : 是否為空的對象

        error() : 拋出異常

        parseHTML() : 解析節(jié)點

        parseJSON() : 解析JSON

        parseXML() : 解析XML

        noop() : 空函數(shù)

        globalEval() : 全局解析JS

        camelCase() : 轉(zhuǎn)駝峰

        nodeName() : 是否為指定節(jié)點名(內(nèi)部)

        each() : 遍歷集合

        trim() : 去前后空格

        makeArray() : 類數(shù)組轉(zhuǎn)真數(shù)組

        inArray() : 數(shù)組版indexOf

        merge() : 合并數(shù)組

        grep() : 過濾新數(shù)組

        map() : 映射新數(shù)組

        guid : 唯一標(biāo)識符(內(nèi)部)

        proxy() : 改this指向

        access() : 多功能值操作(內(nèi)部)

        now() : 當(dāng)前時間

        swap() : CSS交換(內(nèi)部)

        });

        jQuery.ready.promise = function(){}; 監(jiān)測DOM的異步操作(內(nèi)部)

        function isArraylike(){} 類似數(shù)組的判斷(內(nèi)部)

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        深度解析jquery中的核心功能

        深度解析jquery中的核心功能:核心功能包括:jquery是如何定義的,如何調(diào)用的,如何擴(kuò)展的。掌握核心方法是如何實現(xiàn)的,是理解jQuery源碼的關(guān)鍵。這里理解了一切豁然開朗。1,如何定義,即入口// Define a local copy of jQueryvar jQuery = function( selector
        推薦度:
        標(biāo)簽: 里面 的核心 剖析
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲视屏在线观看| 亚洲一二成人精品区| 亚洲中文字幕AV每天更新| 人妻无码一区二区三区免费| 亚洲AV永久无码精品水牛影视 | 国产一级理论免费版| 亚洲精品成a人在线观看夫| 猫咪社区免费资源在线观看 | 国产亚洲午夜精品| 日本不卡视频免费| 免费又黄又爽又猛大片午夜| 国产一区二区三区免费视频 | 亚洲国产精品无码久久一区二区| av午夜福利一片免费看久久| 亚洲色成人WWW永久网站| 国产国拍亚洲精品福利| 波霸在线精品视频免费观看| 亚洲国产精品无码久久久秋霞2 | 国产免费变态视频网址网站| 美景之屋4在线未删减免费| 亚洲国产精品人人做人人爽 | 91精品成人免费国产片| 亚洲娇小性xxxx| 成人国产mv免费视频| 国产高潮流白浆喷水免费A片 | 人人狠狠综合久久亚洲88| 7m凹凸精品分类大全免费| 亚洲人成网站18禁止久久影院 | 亚洲视频一区二区| 日本免费中文视频| 亚洲国语在线视频手机在线| 妞干网手机免费视频| 免费的黄网站男人的天堂| 亚洲精品国产成人片| 91在线视频免费看| 国产精品免费观看视频| 亚洲成人免费网站| 国产成人高清精品免费软件| 成人无码区免费A∨直播| 亚洲中文字幕久久精品无码2021| 免费观看国产小粉嫩喷水|