https://www.youtube.com/watch?v=qeMFEz_ufZc

http://stackoverflow.com/questions/7194784/analysing-the-jquery-source

http://www.programering.com/a/MDMxkDNwATg.html

1, Summary
JQuery is a very good Js library, and prototype, YUI, Mootools etc. compared to many Js class library, it Zoupian Jian Feng, starting from the web development and the practical point of view, in addition to see but not throw some practical things, provide a small library for developers. Due to its short, simple and convenient to use, relatively efficient performance. Many developers choose Jquery to do web development assistance.
Development using jQuery, we often encounter many problems, but the jQuery code is quite obscure, difficult to understand, when the development problems, cannot read the source code, do not know how to debug.
John Resig, The Jquery developers, wrote two books, Pro Javascript Techniques can be regarded as the analysis of Jquery source code. The analysis of the source code, from basic to compare the current library has a great help. The other one is jQuery.in.Action. This book is mainly about how to use Jquery. All two books explain profound theories in simple language. There is a book edited by a pulling JqueryAPI Chinese document. The three is of great help for the master Jquery.

2, init()Analysis
In the analysis of the init (), we have to understand the design concept of jQuery.
Jquery is standing in the developer's point of view to consider the issue, when using Js, most of the time are the operation of the Dom elements, such as modify the properties of elements, amendment, modification of CSS. But the Dom elements, such as getElementsByTag, may take to the collection of Dom elements, and happen to be the set of all the elements have the same operation. If only one element, can be seen as only one element in the collection.
So long as the set of operations, will operate on each element of the collection. JQuery is based on the set and provides a practical method for many, contains the required daily development function. For this collection, we called the jQuery object.
We can through the $(params) or jQuery (params) to generate a Jquery object. In the Jquery document provides four ways: jQuery (expression, [context]), jQuery (HTML), jQuery (elements), jQuery (callback) four kinds of Google jQuery object mode. In fact, the parameters of the Jquery can be any of the elements, such as air parameters, can constitute a jQuery object.
Then jQuery is how to achieve it?
①②③④⑤⑥⑦⑧⑨⑩
var jQuery = window.jQuery = window.$ = function(selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init(selector, context); ①
};
You can see that actually is new init(selector, context):
init : function(selector, context) {
selector = selector || document;// Determination of selector
// The first case of Handle $(DOMElement) of a single Dom element, ignore context
if (selector.nodeType) { ②
this[0] = selector;
this.length = 1;
return this;
}
if (typeof selector == "string") {//Selector string ③
var match = quickExpr.exec(selector);
if (match && (match[1] || !context)) {
if (match[1])// Second kinds of treatment$(html) -> $(array) ④
selector = jQuery.clean([match[1]], context);
else {// Third cases: HANDLE: $("#id") // processing$("#id")
var elem = document.getElementById(match[3]);
if (elem) {
// IE returns the name=id elements, if so, on the document.find(s)
if (elem.id != match[3]) ⑤
return jQuery().find(selector);
// Build a new jQuery(elem)
return jQuery(elem); ⑥
}
selector = [];
}
} else
// Fourth kinds of circumstances: Processing$(expr, [context])==$(content).find(expr)
return jQuery(context).find(selector); ⑦
} else if (jQuery.isFunction(selector)) ⑧ // Fifth cases: $(function) seven Shortcut for document ready
return jQuery(document)[jQuery.fn.ready ? "ready" : "load"](selector);
// Sixth kinds of circumstances: Processing$(elements)
return this.setArray(jQuery.makeArray(selector)); ⑨
},

The above code can be seen $(XX) or Jquery (XX) object is not the real jQuery function generated, but the jQuery.fn.init function generated object. Also is the jQuery object inheritance is the prototype of the jQuery.fn.init. jQuery.fn = jQuery.prototype={..}. We basically do not have to new jQuery (XX), but jQuery (XX), is the use of new jQuery (XX), Mr. into object jQuery function, the inheritance in the prototype, object returned is jQuery.fn.init function generation. And the jQuery function also abandoned. Visible to the jQuery.prototype method for adding has little significance. Also can be seen by new jQuery (XX) less efficient. JQuery.fn.init is through the jQuery.fn.init.prototype = jQuery.fn; to get. In the extended jQuery, as long as the related function of extend to jQuery.fn can be.
JQuery.fn.init is responsible for the parameters of the transmission is analyzed and then generate jQuery objects. The first parameter is divided into four cases:
Type
Explain
Dom Element

The first parameter is the Dom element, do not have second parameters. Directly to the collection of new generation Dom element of a jQuery object. Returns the jQuery object. Construction of jQuery object.

string

The first parameter is the string of three cases:
1, Tag string HTML, $(HTML) $(-> array), the optional second parameter.
Implementation of selector = jQuery.clean ([match[1]], context);. The statement is a hteml string into an array of DOM objects. Then return the Array type.
2, The string is #id$(id)
First, through the VaR elem = document.getElementById (match[3]); elem, as did not take selector = []; to perform a Array type returns an empty collection of jQuery objects.
Such as found by return elem, jQuery (elem); to generate jQuery objects, this is
Returns the Dom type of Element jQuery objects.
3, Compatible with selector string CSS1-3 syntax, second parameters are optional.
Implementation of return jQuery(context).find(selector);. The statement is the first implementation of jQuery(context). We can see that the context second parameters can be of any value, can be a form of collective. After find (selector) jQuery (context) is found in all DOM elements have the collection of DOM elements selector expression, constructing a new jQuery object, and returns.
#ID in fact, and this way is a unified, single out is to improve the performance of.

Fn

The first parameter is a function of. No second parameters. Is the $(document).Ready (FN) of the abbreviation, its returnjQuery (document) [jQuery.fn.ready? "Ready": "load"] (selector) is the implementation of the code. This statement first execute jQuery (document), it is once again the newjQuery.fn.init function, to generate a jQuery object (element is docume

Posted by Christy at July 18, 2014 - 10:03 AM

Jquery 源码学习的更多相关文章

  1. jquery源码学习笔记三:jQuery工厂剖析

    jquery源码学习笔记二:jQuery工厂 jquery源码学习笔记一:总体结构 上两篇说过,query的核心是一个jQuery工厂.其代码如下 function( window, noGlobal ...

  2. jquery源码学习(一)——jquery结构概述以及如何合适的暴露全局变量

    jQuery 源码学习是对js的能力提升很有帮助的一个方法,废话不说,我们来开始学习啦 我们学习的源码是jquery-2.0.3已经不支持IE6,7,8了,因为可以少学很多hack和兼容的方法. jq ...

  3. jQuery源码学习感想

    还记得去年(2015)九月份的时候,作为一个大四的学生去参加美团霸面,结果被美团技术总监教育了一番,那次问了我很多jQuery源码的知识点,以前虽然喜欢研究框架,但水平还不足够来研究jQuery源码, ...

  4. 读艾伦的jQuery的无new构建,疑惑分析——jquery源码学习一

    背景: 有心学习jquery源码,苦于自己水平有限,若自己研究,耗时耗力,且读懂之日无期. 所以,网上寻找高手的源码分析.再经过自己思考,整理,验证.以求有所收获. 此篇为读高手艾伦<jQuer ...

  5. jquery 源码学习(一)

    从上边的注释看,jQuery的源码结构相当清晰.条理,不像代码那般晦涩和让人纠结   1. 总体架构 1.1 自调用匿名函数 self-invoking anonymous function 打开jQ ...

  6. jquery 源码学习(*)

    最近在做日志统计程序,发现对方的程序是在Jquery基础上进行开发的,而公司的网站的框架是prototype.而且我也早就想了解一下Jquery源码,故决定研究Jquery源码,模拟它的方法   Jq ...

  7. 菜鸟的jQuery源码学习笔记(前言)

    前言 相信任何一名前端开发人员或者是前端爱好者都对jQuery不陌生.jQuery简单易用,功能强大,特别是拥有良好的浏览器兼容性,大大降低了前端开发的难度,使得前端开发变得“平易近人起来”.自从本人 ...

  8. jQuery源码学习扒一扒jQuery对象初使化

    神奇的jQuery可以这样玩jQuery("#id").css()或 jQuery("#id").html() 这么玩jQuery("#id" ...

  9. jQuery源码学习笔记一

    学习jQuery源码,我主要是通过妙味视频上学习的.这里将所有的源码分析,还有一些自己弄懂过程中的方法及示例整理出来,供大家参考. 我用的jquery v2.0.3版本. var rootjQuery ...

  10. jquery 源码学习(四)构造jQuery对象-工具函数

    jQuery源码分析-03构造jQuery对象-工具函数,需要的朋友可以参考下.   作者:nuysoft/高云 QQ:47214707 EMail:nuysoft@gmail.com 声明:本文为原 ...

随机推荐

  1. byte数组如何转为short数组 (转)

    最近在搞毕业设计,做的是有关语音识别的手机应用.在处理音频的过程中,发现需要用short数组处理音频,可能光用byte会越界.但是java读文件没有一次性读到short数组中的api,要么是一个一个读 ...

  2. 静默方式安装window oracle

    1. setup.exe -silent -responseFile "C:\app\software\WIN64_11204\RSP\db_install_window.rsp" ...

  3. Interview with Oleg

    Interview with Oleg time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. OpenGL一些函数详解(二)

    OpenGL ES顶点数据绘制技巧 在OpenGL中,绘制一个长方体,需要将每个顶点的坐标放在一个数组中.保存坐标时有一些技巧(由于字母下标不好表示,因此将下标表示为单引号,如A1将在后文中表示为A' ...

  5. POJ 3468<线段树,区间add>

    题目连接 //位运算 k<<1 相当于 k*2 k<<1|1 相当于 k*2+1 /* 修改区间内的值,并且维护区间和. 详见代码 */ #include<cstdio& ...

  6. Hibernate的dynamic-insert和dynamic-update的使用

    Hibernate在初始化的时候,默认按照配置为表预定义insert,delete,update,select(by id)的SQL语句放在session中,其中insert,update,selec ...

  7. 快学Scala-第一章 基础

    知识点: Scala程序并不是一个解释器,实际发生的是,你输入的内容被快速的编译成字节码,然后这段字节码交由Java虚拟机执行. 以val定义的值是一个常量,以var定义的值是一个变量,声明值或变量但 ...

  8. c# 去除文本的html标签

    public static string ContentReplace(string input) { input = Regex.Replace(input, @"<(.[^> ...

  9. dom4j基本使用用法

        DOM4J是dom4j.org出品的一个开源XML解析包,它的网站中这样定义: Dom4j is an easy to use, open source library for working ...

  10. POJ 1236 Network of Schools (tarjan算法+缩点)

    思路:使用tarjan求强连通分量并进行缩点,判断所有入度为0的点,这个点就是必须要给予文件的点,分别计算出度,入度为零的点的个数,取二者的最大值就是把这个图变成强连通需要加的边数. 一个取值需要讨论 ...