The origin of the article: the summary of jquery source Learning
In JS, the general object-oriented notation is as follows:
function The Cao () {}// defines a constructor function () {}// on the prototype Add initialization method function() {} // other practical methods to perform New Cao (); // Instantiate Object c1.init (); // Initialize c1.css (); // operation function
It is cumbersome to define an object Cao, instantiate C1,c1.init () initialization, and C1.other () to invoke other available methods, so that each call needs to instantiate C1 and then initialize C1.init () before other methods can be called.
jquery's approach is to:
function jQuery () {returnnewfunctionfunction( {}= jquery.prototype;//This sentence can be ignored first, the following will be said JQuery (). other ();
Only one row
JQuery (). other ();
The code has done all the work, why?
Under Analysis:
Calling jquery () actually completes the instantiation and initialization process, because jquery () returns the initialization object of JQuery.prototype.init ().
Jquery->jquery.prototype
Jquery->jquery.prototype->jquery.prototype.init
Like the above relationship, the returned new JQuery.prototype.init (), on the prototype of JQuery, JQuery.prototype.init () does not have the other () method at all, How do you get JQuery.prototype.init () to operate other ()?
So there is this sentence:
JQuery.prototype.init.prototype = Jquery.prototype;
The phrase is to assign a reference to the jquery prototype to the JQuery.prototype.init prototype, as shown here:
Jquery->jquery.prototype
Jquery->jquery.prototype->jQuery.prototype.init,jQuery.prototype.init. prototype- >jquery.prototype
This gives a reference to the jquery prototype on the JQuery.prototype.init prototype.
The JQuery.prototype.init () returned by jquery () actually contains everything on the jquery object, so jquery (). Other () is the direct manipulation of other () on the jquery object.
Modifying a jquery prototype is a prototype that modifies the initialization init object.
The actual wording of the jquery source code:
#61-#64行:
function (Selector, context) { // The JQuery object is actually just the Init constructor ' enhanced ' returnnew jQuery.fn.init (selector, context, rootjquery);},
What the hell is JQuery.fn.init?
There is such a word in #96:
Jquery.fn = Jquery.prototype = {...}
Description Jquery.fn is Jquery.prototype, the jquery prototype.
In #283:
JQuery.fn.init.prototype = Jquery.fn;
is JQuery.prototype.init.prototype = Jquery.prototype;
JavaScript Object-oriented notation and jquery object-oriented writing