看了一下午还是有很多地方没弄明白,jQuery的一些工具方法的原理也不完全清楚,这篇文章会随着我深入阅读jQuery源码的同时不断更新。

// Initialize a jQuery object
// 初始化Jquery对象 // A central reference to the root jQuery(document)
var rootjQuery, //
Use the correct document accordingly with window argument (sandbox)
document = window.document, // 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 <)
// 检测html字符串的正则表达式
rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/, init = jQuery.fn.init = function( selector, context ) {
var match, elem; // HANDLE: $(""), $(null), $(undefined), $(false)
if ( !selector ) {
return this;
} // Handle HTML strings
// 下面的match中,match[0]存放原字符串,match[1]存html标签,match[2]存#q中的“q”
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
//selector是HTML标签,类似<div>123</div>
match = [ null, selector, null ]; } else {
//不是html标签
match = rquickExpr.exec( selector );
} // Match html or make sure no context is specified for #id
// 匹配的html或确保没有上下文指定为# id
if ( match && (match[1] || !context) ) { // HANDLE: $(html) -> $(array)
// 1.处理html标签
if ( match[1] ) {
context = context instanceof jQuery ? context[0] : context; // scripts is true for back-compat
// Intentionally let the error be thrown if parseHTML is not present
//jQuery.merge( first, second ) 合并两个数组内容到第一个数组
//jQuery.parseHTML()函数用于将HTML字符串解析为对应的DOM节点数组
//对于HTML文档来说,documentElement是<html>标签对应的Element对象,ownerDocument是document对象
jQuery.merge( this, jQuery.parseHTML(
match[1],
context && context.nodeType ? context.ownerDocument || context : document,
true
) ); // HANDLE: $(html, props)
// rsingleTag匹配匹配独立标签,比如'<html></html>'或者'<div></div>'
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)
//2.处理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, $(...))
//3.处理表达式
} else if ( !context || context.jquery ) {
return ( context || rootjQuery ).find( selector ); // HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
//4.处理有上下文的表达式
} else {
return this.constructor( context ).find( selector );
} // HANDLE: $(DOMElement)
//5.处理DOM元素
} else if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this; // HANDLE: $(function)
// Shortcut for document ready
//6.处理函数
} else if ( jQuery.isFunction( selector ) ) {
return typeof rootjQuery.ready !== "undefined" ?
rootjQuery.ready( selector ) :
// Execute immediately if ready is not present
selector( jQuery );
} if ( selector.selector !== undefined ) {
this.selector = selector.selector;
this.context = selector.context;
} return jQuery.makeArray( selector, this );
};
 

【jQuery源码】jQuery对象初始化的更多相关文章

  1. JQuery源码之“对象的结构解析”

    吃完午饭,觉得有点发困,想起了以后我们的产品可能要做到各种浏览器的兼容于是乎不得不清醒起来!我们的web项目多数是依赖于Jquery的.据了解,在Jquery的2.0版本以后对IE的低端版本浏览器不再 ...

  2. jquery源码'jQuery.fn.init.prototype'

    一般我们在创建构造函数即使用的时候会这样写,使用的时候会使用new 关键字,先实例化,然后使用. function test(name, age) { this.name = name; this.a ...

  3. jQuery源码解析对象实例化与jQuery原型及整体构建模型分析(一)

    //源码剖析都基于jQuery-2.0.3版本,主要考虑到兼容IE 一.关于jQuery对象实例化的逻辑: 整个jQuery程序被包裹在一个匿名自执行行数内: (function(window,und ...

  4. jQuery源码-jQuery.fn.attr与jQuery.fn.prop

    jQuery.fn.attr.jQuery.fn.prop的区别 假设页面有下面这么个标签,$('#ddd').attr('nick').$('#ddd').prop('nick')分别会取得什么值? ...

  5. 读jQuery源码 jQuery.data

    var rbrace = /(?:\{[\s\S]*\}|\[[\s\S]*\])$/, rmultiDash = /([A-Z])/g; function internalData( elem, n ...

  6. JQuery源码解析-- 对象的创建

    使用 $("a") 返回的对象就不再是一个简单的DOM对象了,而是一个复杂的JQuery对象. 那么JQuery是怎么创建对象的. 为了便于分析,我将JQuery中复杂的代码简化了 ...

  7. JQuery源码-------JQuery中数值型变量的判断isNumeric

    判断一个数值型变量的方法,在jquery中非常简单,只有一行代码. isNumeric: function( obj ) { // parseFloat NaNs numeric-cast false ...

  8. jQuery源码的一个坑

    纯吐槽 大半夜也真是够了,想学着jQ造个小轮子巩固下js,结果一开始就卡住了. 虽然之前也看过源码,但是主要是研究方法实现什么的,对于框架主函数和入口结构不怎么熟悉,于是想着一步一步调试看看. $(' ...

  9. jquery 源码解析

    静态与实力方法共享设计 遍历方法 $(".a").each() //作为实例方法存在 $.each() //作为静态方法存在 Jquery源码 jQuery.prototype = ...

  10. jQuery源码解析资源便签

    最近开始解读jQuery源码,下面的链接都是搜过来的,当然妙味课堂 有相关的一系列视频,长达100多期,就像一只蜗牛慢慢爬, 至少品读三个框架,以后可以打打怪,自己造造轮子. 完全理解jQuery源代 ...

随机推荐

  1. 第二届普适计算和信号处理及应用国际会议论文2016年 The 2nd Conference on Pervasive Computing, Signal Processing and Applications(PCSPA, 2016)

    A New Method for Mutual Coupling Correction of Array Output Signal 一种阵列输出信号互耦校正的新方法 Research of Robu ...

  2. python与JavaScript中正则表达式如何转换

    使用python爬取网站数据的时候,总会遇到各种各样的反爬虫策略,有很大一部分都和JavaScript(以下简称为JS) 有关.在破解这些JS代码的过程中,经常会遇到模拟JS正则表达式的情况,因此,今 ...

  3. HDU1349 Minimum Inversion Number 2016-09-15 13:04 75人阅读 评论(0) 收藏

    B - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  4. codevs 1083

    这道题是看了人家大牛的解题报告: 对了,要说明一下,(A+B)&1 ,表示,判断(A+B)是奇数否? 下面给出代码: #include<iostream> #include< ...

  5. 团队项目第六周——Alpha阶段项目复审

    排名 队名 项目名 优点 缺点 1 大猪蹄子队 四六级背单词游戏 功能开发出来了,界面简洁美观. 功能的确开发出来了,但是还未完成整个程序.不过考虑到开发时长问题,可以理解.页面还是比较简洁,但是测试 ...

  6. [转载]HTML5游戏前端开发秘籍

    http://isux.tencent.com/html5-game-development-cheats.html 转载至腾讯ISUX HTML5游戏前端开发秘籍 本文由米随随编写 QQ空间Andr ...

  7. PO Release Final Closed 灾难恢复

    今天不小心 Final Closed了一条Po Release,只能通过后台更新数据恢复了. 更新后可接收可匹配,但不保证更新数据有遗漏,慎用. 更新前备份各表数据 UPDATE PO_LINE_LO ...

  8. WPF/Silverlight开发的15个最佳实践(转发)

    英文出处:http://www.kunal-chowdhury.com/2010/08/some-best-practices-for-silverlight.html 作者:Kunal Chowdh ...

  9. [LeetCode] Climbing Stairs (Sequence DP)

    Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...

  10. 微信小程序支付C#后端源码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...