Complete comments of jQuery1.0 source code analysis Constructor (5)

Source: Internet
Author: User

 

JQuery's function design is very compact and there are many calls to each other. For example, you need to understand function a and find that function B is called in function a, and Function c is called in function B. In this way, the system will basically be dizzy.

 

Therefore, I suggest reading a function and a function. Do not follow it up. After reading all the articles, you will naturally get through.

 

 

So today I will annotate jQuery constructor.

 

 

Function jQuery (a, c ){

 

// Process the ready function, $ (function (){})

If (a & a. constructor = Function & jQuery. fn. ready)

Return jQuery (document). ready ();

 

// Set a default value for

A = a | jQuery. context | document;

 

// If a is a jQuery object, merge a and null arrays and return the result. The purpose of this operation is not to destroy the original jQuery object.

// (Note: The jquery attribute is available for each jQuery object and its value is the jQuery version)

If (a. jquery)

Return $ (jQuery. merge (a, []);

 

// If c is a jQuery object, call the find function to find

If (c & c. jquery)

Return $ (c). find ();

// None of the above cases. Start the new jQuery object.

If (window = this)

Return new jQuery (a, c );

 

// If a is an html code, $ ('<div/>') converts the html code into a Dom element.

// JQuery. clean converts html code into a Dom element array.

Var m =/^ [^ <] * (<. +>) [^>] * $/. exec ();

If (m) a = jQuery. clean ([m [1]);

 

// If array a is an array or an array of classes containing dom elements, merge array a and null.

// In other cases, call the find function. The find function is used to process css expressions.

// Call the get method to return the jQuery object.

This. get (a. constructor = Array | a. length &&! A. nodeType & a [0]! = Undefined & a [0]. nodeType?

// Assume that it is an array of DOM Elements

JQuery. merge (a, []):

 

// Find the matching elements and save them for later

JQuery. find (a, c ));

 

// Check whether the last parameter is a Function. If yes, traverse and execute it.

Var fn = arguments [arguments. length-1];

If (fn & fn. constructor = Function)

This. each (fn );

}

 

Finally, from the above constructor code, jQuery constructor mainly supports the following methods:

 

 

1. $ (Function), ready Function

 

2. $ (Element)/$ ([Element]) can directly convert Dom elements or arrays into jQuery objects.

3. $ (CSS Expression, Content). You can also use CSS expressions to select Dom elements.

 

4. $ (Html) and html can also be converted to jQuery objects.

 

 


Author: baozhifei

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.