How jquery is structured. jquery is an architecture
I have always had a question.
Jquery can be used as a method, for example, $ (), or $. extend () for an object ();
Let's summarize it now.
Function method () {} var m = new method (); m. version = "123"; console. log (m. version) // print 123
After declaring an object, you can add attributes or methods to the object.
Continue to look down:
var m=function(){};m.version="123";
This area can still print 123;
In this lesson, no object is stated, but a variable method name is defined.
Core 1: in fact, the variable method name is also an object
For example, the above Code is essentially like this.
var m=new Function();m.version="123";
This is why jQuery can directly sit on objects.
Next, let's see why jQuery can be used directly.
Core 2:If the constructor returns a value, the object created by the new operator is discarded. The returned value is the value of the new expression.
Function Method (name, age) {var obj = {name: name, age: age };
This. name = "I am from myself"; return obj;} var objM = new Method ("Dr. Sisi", 26); console. log (objM); // print the obj object without printing "I'm from myself"
With the above two core theories, we can build jQuery.
(Function (window, undefined) {var jQuery = (function (){
// Core theory 2 var jQuery = function (name, age) {return new jQuery. prototype. init (name, age) ;}; jQuery. prototype = {init: function (name, age) {return [name, age] ;}}; jQuery. prototype. init. prototype = jQuery. prototype;
// Core theory 1. jQuery. version = "1.7.2"; return jQuery;}) (); window. $ = window. jQuery = jQuery;}) (window );
Console. log (jQuery. version );
Console. log (jQuery ("Dr. Sisi", 25 ))
The above code imitates jQuery as a method and the basic theory used for objects.