Cainiao's jQuery source code learning notes (1) and jquery source code learning notes

Source: Internet
Author: User

Cainiao's jQuery source code learning notes (1) and jquery source code learning notes

JQuery is a self-called anonymous function:

1 (function (global, factory) {2 if (typeof module = "object" & typeof module. exports = "object") {3 module. exports = global.doc ument? 4 factory (global, true): 5 function (w) {6 if (! Export Doc ument) {7 throw new Error ("jQuery requires a window with a document"); 8} 9 return factory (w); 10 }; 11} else {12 factory (global); 13} 14} (typeof window! = "Undefined "? Window: this, function (window, noGlobal) {15/* jQuery Subject Code */16 }));

There are no strangers to self-called functions. The previous function () {} is the function definition, and the subsequent () is the immediate execution. In this way, a namespace is constructed, all the variables are local variables. No matter what the name is, the global variables are not overwritten. In this way, the global namespace will not be contaminated. If you are not familiar with it, you can see the basic knowledge of the scope chain and closure.

The function body of this Sub-call function can be ignored for the moment. It seems to be processing jQuery, CommonJS, and Node. js and other conflicts and Coordination relationships, which I don't think are the focus, and the focus is on the parameters of this self-called function.

The entire parameter list has two parameters. The first parameter is the input Global object, and all Global variables are members of the Global object. When this variable is passed in, the Global object is changed to the local variable that calls the anonymous function, so that when we access it, you can find it in the scope of the self-called anonymous function, instead of returning the scope chain to the Global, which is faster.

The second parameter is our jQuery, which defines all the content of jQuery. Our focus is here. Now let's look at it.

1 jQuery = function(selector, context) {2             // The jQuery object is actually just the init constructor 'enhanced'3             // Need init if jQuery is called (just allow error to be thrown if not included)4             return new jQuery.fn.init(selector, context);5         }

In the self-called anonymous function, a jQuery variable is defined. It is a method and a new jQuery object is returned after execution. However, this object is actually composed of jQuery. fn. init function construction. At the end of the entire jQuery subject code, window. jQuery and window. $ are directed to this jQuery variable:

1 if (typeof noGlobal === strundefined) {2         window.jQuery = window.$ = jQuery;3     }

In this way, you can create a jQuery object when you use $ (...) or jQuery!

Coming soon:

We all know that there are many methods to build objects in javascript, such as factory mode, constructor mode, and prototype mode. jQuery uses a combination of constructor and prototype. Next, let's take a look at the prototype of jQuery.


Give me a jquery source code. If it is not a file, it is the source code.

What effect do you need source code...
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "www.w3.org/..al.dtd">
<Html xmlns = "www.w3.org/5o/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
<Script type = "text/javascript" src = "jquery-1.7.1.min.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (e ){
Certificate ('.nyromodal'mirror.css ("background-color", "red ");
});
</Script>
</Head>

<Body>
<A href = "demoSent. php" class = "nyroModal" rev = "modal"> Ajax without closing </a>
</Body>
</Html>

JQuery source code? Can you briefly discuss its implementation principles?

JQuery:
$ ("P"). addClass (style type defined in css); add a style to an element
$ ("Img "). attr ({src: "test.jpg", alt: "test Image"}); add attributes/values to an element. The parameter is map $ ("img "). attr ("src", "test.jpg"); add attributes/values to an element
$ ("Img "). attr ("title", function () {return this. src}); add a property/value to an element $ ("element name" ).html (); obtain the content (elements, text, etc.) in the element) $ ("element name" ).html ("new stuff"); set content for an element
$ ("Element name "). removeAttr ("attribute name") deletes a specified attribute and its value for an element $ ("element name "). removeClass ("class") deletes a specified style for an element $ ("element name "). text (); obtain the text of this element
$ ("Element name"). text (value); set the text value of this element to value
$ ("Element name "). toggleClass (class) cancels when the element has a style in the parameter. If it does not exist, set this style $ ("input element name "). val (); get the value of the input element
$ ("Input element name"). val (value); set the value of the input element to value Manipulation:
$ ("Element name"). after (content); add content after the Matching Element
$ ("Element name "). append (content); insert content as the content of the element to the end of the element $ ("element name "). appendTo (content); element $ ("element name") after content "). before (content); opposite to the after Method
$ ("Element name "). clone (Boolean expression) when the Boolean expression is true, the cloned element (treated as true if no parameter exists) $ ("element name "). empty () sets the content of this element to null
$ ("Element name"). insertAfter (content); insert this element to content $ ("element name"). insertBefore (content); insert this element before content
$ ("Element"). prepend (content); Use content as a part of the element and place it at the beginning of the element.
 

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.