jquery Framework Analysis Chapter One: the first anonymous function

Source: Internet
Author: User

My jquery version is 1.7*

This version of the code than the previous version of the optimization of a lot, the structure is also clear a lot, with the latest bar.

Open jquery Source code

First you can see that all the code is a

(function (window,undefined) {}) (window)

What is this thing?

First, this is an anonymous function (does not understand?) Let's go to Google first);

Second, this is a self-invoking anonymous function;

This piece of code does three things

First: Create a space for jquery only, we estimate and call it "private space", similar to the C + + namespace, in order to ensure that jquery does not conflict with other JavaScript frameworks, if it is a JavaScript framework, for versatility, Must have its own namespace!

Second: Passing in a window variable, the window is a global variable, but it is passed to the jquery framework, and this time window becomes a local variable in the jquery frame, why? Everything for speed. When window becomes a local variable, the window variable is called within the JQ framework, and its function is not to be returned to the top layer.

Note: The Window object that is uploaded to jquery is not a copy, but can be considered a pointer, pointing to the same memory address.

As in the following code:

Window.a= ' This was in the window ';(function (window,undefined) {window.a= ' This was in    JQ ';    SetTimeout (function () {        alert (WINDOW.C);    Output This is C    -},1000)}) (window) window.c= ' this is C '; Console.log (WINDOW.A)   //Output this was in JQ;

The third piece: Incoming undefined variable, why?

I'm not sure, I checked the information and said it was to ensure that the undefined in the jquery space is not defined. Because undefined can be assigned to a local variable.

Such as:

Undefined= "This is Siren"; alert (undefine);

This code in addition to the Firefox can not change, the other browser unefined has become defined.

Next chapter, we will be from the jquery source, from the top down a little analysis.

jquery Framework Analysis Chapter One: the first anonymous function

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.