本文还可在github上查看,可点击这里~~~~

jQuery源码-美元$的若干种使用方法

学习jQuery源码,第一步是了解jQuery整体核心代码结构。第二步,当然就是了解无比强大无所不能的美元$。根据平常使用jQuery的经验,你会发现,几乎所有的语句都是以美元开头的,比如:

$(function(){

    console.log('dom ready 啦!!');

});

又比如:

$('#casper').addClass('handsome');

当然还有其他。。。翻开jQuery的源码你会发现,里面就一行代码:

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的究竟:里面究竟是什么东东,能够让美元符号$如此强大以至于无处不在。

初探jQuery.fn.init

老规矩,打开编辑器,定位到jQuery.fn.init这个方法。如果是用sublime的话,可以试下ctrl+r,然后输入init,第一个出来的搜索结果就是。

相信很多童鞋跟我的第一反应是:oh my god!将近90行代码!不过还可以接受啦,工作中还见别人写过300++行的方法,想想90行也算不得可怕。

然而,当你再往下看,可能就会有种想死的心——怎么这么多if、else!

不卖关子之所以会有那么多if、else,是因为——$有将近10种用法,文章最开头列举的不过是最常见的两种用法而已。

以下为jQuery.fn.init的源码,瞄一眼感受下这代码的可怕就可以了,可以暂时忽略其中的实现细节,安心进入下一节。

init: function( selector, context, rootjQuery ) {

    var match, elem;

 

    // HANDLE: $(""), $(null), $(undefined), $(false)

    if ( !selector ) {

        return this;

    }

 

    // Handle HTML strings

    if ( typeof selector === "string" ) {

        if ( selector.charAt(0) === "<" && selector.charAt( 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;

 

                // scripts is true for back-compat

                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] );

 

                // Check parentNode to catch when Blackberry 4.6 returns

                // nodes that are no longer in the document #6963

                if ( elem && elem.parentNode ) {

                    // Handle the case where IE and Opera return items

                    // by name instead of ID

                    if ( elem.id !== match[2] ) {

                        return rootjQuery.find( selector );

                    }

 

                    // Otherwise, we inject the element directly into the jQuery object

                    this.length = 1;

                    this[0] = elem;

                }

 

                this.context = document;

                this.selector = selector;

                return this;

            }

 

        // HANDLE: $(expr, $(...))

        } else if ( !context || context.jquery ) {

            return ( context || rootjQuery ).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.context = this[0] = selector;

        this.length = 1;

        return this;

 

    // HANDLE: $(function)

    // Shortcut for document ready

    } else if ( jQuery.isFunction( selector ) ) {

        return rootjQuery.ready( selector );

    }

 

    if ( selector.selector !== undefined ) {

        this.selector = selector.selector;

        this.context = selector.context;

    }

 

    return jQuery.makeArray( selector, this );

},
jQuery.fn.init源代码

相关文章:

  • 2021-06-10
  • 2021-12-27
  • 2021-07-02
  • 2021-12-25
  • 2021-05-24
  • 2022-12-23
  • 2021-11-29
  • 2022-02-11
猜你喜欢
  • 2021-11-07
  • 2022-12-23
  • 2022-02-28
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
相关资源
相似解决方案