jQuery源碼分析init

init的功能就是初始化jquery對(duì)象 Initialize a jQuery object

首先jQuery.fn = jQuery.prototype = {}
Query.extend = jQuery.fn.extend

// Define a local copy of jQuery
    jQuery = function( selector, context ) {
        // The jQuery object is actually just the init constructor 'enhanced'
        // Need init if jQuery is called (just allow error to be thrown if not included)
              //init是jQuery原型上面的一個(gè)函數(shù)错森,它也是構(gòu)造函數(shù),返回一個(gè)jQuery的實(shí)例,它經(jīng)常返回this,this指代的是原型
        return new jQuery.fn.init( selector, context );
    },

init函數(shù)經(jīng)常返回this.如果是作為構(gòu)造函數(shù)的話掉瞳,那么this是構(gòu)造函數(shù)新生成的對(duì)象署咽,繼承構(gòu)造函數(shù)所有this的屬性跪妥,有一個(gè)自己的副本饺律。同時(shí)炭序, init.prototype = jQuery.fn;使得新生成的對(duì)象能夠繼承JQuery原型的屬性罚斗。

![Uploading Paste_Image_820586.png . . .]

// Initialize a jQuery object
define( [
    "../core",
    "../var/document",
    "./var/rsingleTag",
    "../traversing/findFilter"
], function( jQuery, document, rsingleTag ) {

"use strict";

// A central reference to the root jQuery(document)
var rootjQuery,

    // A simple way to check for HTML strings
    // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
    // Strict HTML recognition (#11290: must start with <)
    // Shortcut simple #id case for speed
    rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]+))$/,

       //三個(gè)參數(shù)徙鱼,第一個(gè)參數(shù)是選擇器
    init = jQuery.fn.init = function( selector, context, root ) {
        var match, elem;

        // HANDLE: $(""), $(null), $(undefined), $(false)
        if ( !selector ) {
            return this;
        }

        // Method init() accepts an alternate rootjQuery
        // so migrate can support jQuery.sub (gh-2101)
        root = root || rootjQuery;

        // Handle HTML strings
        if ( typeof selector === "string" ) {
            if ( selector[ 0 ] === "<" &&
                selector[ selector.length - 1 ] === ">" &&
                selector.length >= 3 ) {

                // Assume that strings that start and end with <> are HTML and skip the regex check
                match = [ null, selector, null ];

            } else {
                match = rquickExpr.exec( selector );
            }

            // Match html or make sure no context is specified for #id
            if ( match && ( match[ 1 ] || !context ) ) {

                // HANDLE: $(html) -> $(array)
                if ( match[ 1 ] ) {
                    context = context instanceof jQuery ? context[ 0 ] : context;

                    // Option to run scripts is true for back-compat
                    // Intentionally let the error be thrown if parseHTML is not present
                    jQuery.merge( this, jQuery.parseHTML(
                        match[ 1 ],
                        context && context.nodeType ? context.ownerDocument || context : document,
                        true
                    ) );

                    // HANDLE: $(html, props)
                    if ( rsingleTag.test( match[ 1 ] ) && jQuery.isPlainObject( context ) ) {
                        for ( match in context ) {

                            // Properties of context are called as methods if possible
                            if ( jQuery.isFunction( this[ match ] ) ) {
                                this[ match ]( context[ match ] );

                            // ...and otherwise set as attributes
                            } else {
                                this.attr( match, context[ match ] );
                            }
                        }
                    }

                    return this;

                // HANDLE: $(#id)
                } else {
                    elem = document.getElementById( match[ 2 ] );

                    if ( elem ) {

                        // Inject the element directly into the jQuery object
                        this[ 0 ] = elem;
                        this.length = 1;
                    }
                    return this;
                }

            // HANDLE: $(expr, $(...))
            } else if ( !context || context.jquery ) {
                return ( context || root ).find( selector );

            // HANDLE: $(expr, context)
            // (which is just equivalent to: $(context).find(expr)
            } else {
                return this.constructor( context ).find( selector );
            }

        // HANDLE: $(DOMElement)
        } else if ( selector.nodeType ) {
            this[ 0 ] = selector;
            this.length = 1;
            return this;

        // HANDLE: $(function)
        // Shortcut for document ready
        } else if ( jQuery.isFunction( selector ) ) {
            return root.ready !== undefined ?
                root.ready( selector ) :

                // Execute immediately if ready is not present
                selector( jQuery );
        }

        return jQuery.makeArray( selector, this );
    };

// Give the init function the jQuery prototype for later instantiation
init.prototype = jQuery.fn;

// Initialize central reference
rootjQuery = jQuery( document );

return init;

} );
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市针姿,隨后出現(xiàn)的幾起案子袱吆,更是在濱河造成了極大的恐慌,老刑警劉巖距淫,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绞绒,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡榕暇,警方通過查閱死者的電腦和手機(jī)处铛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門饲趋,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人撤蟆,你說(shuō)我怎么就攤上這事奕塑。” “怎么了家肯?”我有些...
    開封第一講書人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵龄砰,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我讨衣,道長(zhǎng)换棚,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任反镇,我火速辦了婚禮固蚤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘歹茶。我一直安慰自己夕玩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開白布惊豺。 她就那樣靜靜地躺著燎孟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪尸昧。 梳的紋絲不亂的頭發(fā)上揩页,一...
    開封第一講書人閱讀 52,682評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音烹俗,去河邊找鬼爆侣。 笑死,一個(gè)胖子當(dāng)著我的面吹牛幢妄,可吹牛的內(nèi)容都是我干的兔仰。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼磁浇,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼斋陪!你這毒婦竟也來(lái)了朽褪?” 一聲冷哼從身側(cè)響起置吓,我...
    開封第一講書人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎缔赠,沒想到半個(gè)月后衍锚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡嗤堰,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年戴质,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了度宦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡告匠,死狀恐怖戈抄,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情后专,我是刑警寧澤划鸽,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站戚哎,受9級(jí)特大地震影響裸诽,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜型凳,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一丈冬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧甘畅,春花似錦埂蕊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至荸实,卻和暖如春匀们,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背准给。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工泄朴, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人露氮。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓祖灰,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親畔规。 傳聞我的和親對(duì)象是個(gè)殘疾皇子局扶,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • 工廠模式類似于現(xiàn)實(shí)生活中的工廠可以產(chǎn)生大量相似的商品,去做同樣的事情叁扫,實(shí)現(xiàn)同樣的效果;這時(shí)候需要使用工廠模式三妈。簡(jiǎn)單...
    舟漁行舟閱讀 7,779評(píng)論 2 17
  • 在線閱讀 http://interview.poetries.top[http://interview.poetr...
    程序員poetry閱讀 114,426評(píng)論 24 450
  • 請(qǐng)參看我github中的wiki,不定期更新莫绣。https://github.com/ivonzhang/Front...
    zhangivon閱讀 7,135評(píng)論 2 19
  • 想想覺得還有點(diǎn)諷刺畴蒲,每天要輸多次手機(jī)密碼1127,看手機(jī)qq空間也有好友生日提醒对室,對(duì)1127一直就特別上心模燥】Ъ溃可到最...
    starJm火龍果閱讀 225評(píng)論 0 0
  • 據(jù)21世紀(jì)經(jīng)濟(jì)報(bào)道,央行基于區(qū)塊鏈的數(shù)字貨幣原型方案已初步確定蔫骂,法定數(shù)字貨幣的原型系統(tǒng)Demo有望在春節(jié)后推出么翰。 ...
    吉盟財(cái)富公司閱讀 294評(píng)論 0 0