The following is a summary of jquery source code learning to be improved...
1. The overall structure of the JS file is as follows: (function () {}) (... omitted row 3548); this should be equivalent to an eval function.
2. Each $ ("") generates a jquery object. The constructor is as follows:
VaR jquery = Window. jquery = Window. $ =
Function (selector, context)
{
Return new jquery. FN. INIT (selector, context );
}
3. jquery. fn = jquery. Prototype = {...}
Assign a value to the prototype of the jquery class. It shares the object with jquery. fn.
Jquery. FN. init. Prototype = jquery. FN; then set the prototype of jquery. FN. init to jquery. FN, that is, jquery. prototype.
The constructor (return New jquery. FN. INIT (selector, context);) returns a jquery object.
4. jquery. Extend = jquery. FN. Extend = function () {...} defines extend. When calling this method, copy the members of the parameter object
In the jquery class. jquery [name] = objet [name];
5. The jquery. Each (object, callback, argS) function is added to the jquery object through the extend method. The call method is as follows:
Jquery. Each ({
Parent: function (ELEM) {return ELEM. parentnode ;},
Parents: function (ELEM) {return jquery. dir (ELEM, "parentnode ");},
Next: function (ELEM) {return jquery. Nth (ELEM, 2, "nextsibling ");},
}, Function (name, FN ){
Jquery. FN [name] = function (selector ){
VaR ret = jquery. Map (this, FN );
If (selector & typeof selector = "string ")
Ret = jquery. multifilter (selector, RET );
Return this. pushstack (jquery. Unique (RET ));
};
});
In jquery. Each, the key is to use callback. Apply (object [name], argS) to make the object [name] object (
For example, function (ELEM) {return ELEM. parentnode;}) Execution Method
Function (name, FN ){
Jquery. FN [name] = function (selector ){
VaR ret = jquery. Map (this, FN );
If (selector & typeof selector = "string ")
Ret = jquery. multifilter (selector, RET );
Return this. pushstack (jquery. Unique (RET ));
};
} To copy the member (method) in the object to jquery. FN, that is, jquery. prototype, so as to increase the method on the jquery object.