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