However, due to work reasons, I have never worked on a website project for a long time, and I have no time to study the Jquery source code. This question has not been solved. Today, I am idle, open the Jquery source code to see how it works tomorrow. When we added the jquery js file, we actually executed a function, $ and JQuery variables have been initialized in this function. The source code for implementing this function is as follows (the code has been deleted and changed, which does not affect the implementation principles ):
Copy codeThe Code is as follows:
(Function (){
Var
// Will speed up references to window, and allows munging its name.
Window = this,
// Will speed up references to undefined, and allows munging its name.
Undefined,
// Map over jQuery in case of overwrite
_ JQuery = window. jQuery,
// Map over the $ in case of overwrite
_ $ = Window. $,
JQuery = window. jQuery = window. $ = function (selector, context ){
// The jQuery object is actually just the init constructor 'enabled'
Return new jQuery. fn. init (selector, context );
},
// A simple way to check for HTML strings or ID strings
// (Both of which we optimize)
QuickExpr =/^ [^ <] * (<(. | \ s) +>) [^>] * $ | ^ # ([\ w-] +) $ /,
// Is it a simple selector
IsSimple =/^. [^: # \ [\.,] * $ /;
JQuery. fn = jQuery. prototype = {
Init: function (selector, context ){
// Make sure that a selection was provided
// Make sure that a selection was provided
Selector = selector | document;
This [0] = selector;
This. length = 1;
This. context = selector;
Return this;
},
Show: function (){
Alert ("this. show ");
},
// Start with an empty selector
Selector :"",
// The current version of jQuery being used
Jquery: "1.3.2"
};
JQuery. fn. init. prototype = jQuery. fn;
})();
Function test (src ){
Alert ($ (src ));
$ (Src). show ();
From the code, we can see that such a function is executed (funtion (){})();
Var window = this;
_ JQuery = window. jQuery;
_ $ = Window. $;
The Code should be the declaration of jQuery and $ variables. As for why I haven't figured it out, please wait for someone else to solve it !!
But I think this does not matter, because the most important thing is the following code:
JQuery = window. jQuery = window. $ = function (selector, context ){
Return new jQuery. fn. init (selector, context );
};
We can see that jQuery is created. fn. A function like init is returned to $, so that you can use the $ instance, but you still cannot access jQuery. therefore, the following sentence must be added:
JQuery. fn. init. prototype = jQuery. fn;
With these implementations, other functions in Jquery are well understood. They are nothing more than methods in prototype or extend.