您的位置:首页 > Web前端 > JQuery

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

2015-12-08 21:17 1056 查看
当我们用$()时就是调用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: