jquery-2.1.4 源码解读(2):jQuery.fn.init 构造函数解读

当我们用$()时就是调用new jQuery.fn.init() 生成jQuery对象。init构造函数在源码的第2735-2832行,具有两个形参,结构如下:

jQuery.fn.init = function(selector, context){
...
}

 init()构造函数对四种情况进行处理:

1、当selector为"",null,undefined,false时,将不带自定义属性的空jQuery对象返回,代码如下:

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

2、当selector为string类型时,又分5种情况,代码从2744-2812:

  1) 当字符串类型为<....>时

  2) 对应(html, props)

  3) 当字符串类型为#...时,此时对应根据id选择符进行选择时

  4) 对应$(expr, $(...))时

  5) 对应 $(expr, context) 时

  其中第四种和第五种情况最为复杂。

3、当selector为DomElement时,代码2812-2824

4、当selector为function时,代码2828-2833

posted @ 2015-12-08 21:17  比昂  阅读(230)  评论(0编辑  收藏  举报