JQuery Source Analysis Note (2) variable list _jquery

Source: Internet
Author: User
_jquery = Window.jquery;
_$ = window.$;
These two variables are the only two global variables that jquery uses. In the Jquery.noconflict () function, the two variables are returned.
For browser detection, jquery uses check useragent instead of feature detection.
Rwebkit =/(WebKit) [\/] ([\w.] +)/,
Ropera =/(Opear) (?:. *version)? [ \/] (\w+)/,
Rmsie =/(MSIE) ([\w.] +)/,
Rmozilla =/(Mozilla) (?:. *? RV: ([\w.] +))?/,
Initialize function init
jquery's processing of JS objects is a bit more round, and the ultimate goal is to turn the results of the jquery selector into an array of similar objects. There are length,first,last and so on. Because $ ("...") is from the DOM tree from the selection of some nodes. However, there are many other features, such as the commonly used $ (function () {...}) Used to initialize the page after loading, $ ("... >...</...> ") to directly get a node to append into the DOM tree.
The next step, starting with 93 lines, is a long init function. Init:function (selector, context, rootjquery)
Steps:
1, selector is illegal parameters: null characters, null and undefined are returned directly to this. The jquery object that has the default properties.
2, selector is domelement. That is, with the original JS, such as getElementById and other elements. Then, it's equivalent to wrapping the native DOM object in $. Place this element in the first position of the internal array and set the length to 1. And then returns.
3, Special optimization processing $ ("body"). That is, the document.body element.
4. Selector is a string that begins with < at the end of >. Then suppose you want to create a new DOM element with a string. such as $ ("<a href= ' http://www.cnblogs.com" > Blog Park </a>). For security reasons, a regular expression is used here to check whether the form of <...>...</...> or #id.
quickexpr =/^ (?: [^<]* (<[\w\W]+>) [^>]*$|# ([\w\-]*) $)/. After Quickexpr.exec (selector), if it is an HTML string, it gets [match, match, undefined], and the #id form gets the result of [#id, undefined, id]. This separates the strings.
For HTML strings, if there is only one tag, then call createelement directly. Otherwise call a createfragment helper function, this function uses createdocumentfragment, and then all tags are inserted.
Createfragment is implemented in line 5892, and here's a notable point is that jquery caches HTML fragments. and for different browsers and elements have different processing, the author wrote a large section of the note. summed up is (1) cache only small fragments less than 0.5KB. (2) The selected state is not cached. (3) The IE6 <object> and <embed> elements are not cached. (4) WebKit does not cache the checked attribute of an element. These are not cached because jquery uses the cloning (clone) node to cache, and 2-4 of the mentioned cases are lost at clone. jquery uses regular, or jquery.support, helper functions for caching policy judgments. Let's skip over here. Jquery.support involves a lot of browser-related issues.
5, if the check in 4 is #id, then directly call document.getElementById
6, if the selector is a function, then consider it a Document.ready event handler function.
7, the rest of the various situations, such as the introduction of the context and so on. Unified call a Find (selector) to handle. This function is discussed again later. (5109 lines, jquery.find = Sizzle; jquery.expr = sizzle.selectors; Indicates that other complex selector expressions are thrown to the sizzle project.
jquery Basic Members
jquery is designed as an object that behaves and sets very much alike. So you need some conversion methods and basic properties.
1, jquery: Version number. The simplest way to get the version number: $ (). jquery
2, length and size (): lengths.
3, ToArray (): Convert to JS array
4, Get (num): Returns the nth element. If passed in null, the result of ToArray () is returned directly. Here is the return of DomElement.
5, Pushstack (): See http://api.jquery.com/pushStack/. A new jquery object is added internally, and then the results of Elems and selector are merged in and then returned to the new jquery object. Here's a set of Prevobject properties, look down the end () function.
6, each (callback, args): Traversing the elements within the array. Internal calls to the $.each utiltiy.
7, Ready (FN): actually and $ (function () {}) is equivalent.
8, eq (i): I allow positive and negative digits, and still return the jquery object, but only one element. It's actually just a slice package.
9, the One () and last (): In fact, eq (0) and EQ (-1), a very simple package.
10, Slice (): According to the parameters to obtain a reference to the part of the array. Internal use of pushstack implementation. So the return is also a new jquery object.
11, Map (callback): Call callback for each element in turn. Maps an array of the original element A to an array of element B. Callback is the mapping function of element a-> element B. A very basic concept in functional programming (FP).
12, End (): See Http://api.jquery.com/end. This is the last state to return the selector. Returns the Jquery.prevobject property. This property is set in the Pushstack function, which sets the prevobject of this new object to this before returning the new jquery object. After such multiple pushstack, it becomes a list (chain). and end () is to follow the list of a node forward. After the selector, the root of the Prevobject is document. such as $ ("Body div"). Prevobject is the document.
Extend function
After the basic members of jquery, all the other members were added with extend. Statement:
Jquery.extend = JQuery.fn.extend = function () {}
Assigns the properties of all object parameters after target to target and specifies whether to make a deep copy if the first argument is a Boolean value. It then returns the modified target (not the new object, the Extend function directly modifies the original object).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.