Init Function Analysis
First, let's take a look at the four types of object construction methods that can be used in jQuery:
L $ (expression, [context]). The content of the string is a CSS selector. For example, msg in $ ("# msg") is the ID of an element.
L $ (html), an HTML string, returns a DOMElement Element Object
L $ (function), ready event processing function
L $ (Object Reference), returns a processed jQuery object
Analyze init
35 jQuery. fn = jQuery. prototype = {
36 init: function (selector, context ){
37 // Make sure that a selection was provided
38 selector = selector | document;
39
40 // Handle $ (DOMElement)
41 if (selector. nodeType ){
42 this [0] = selector;
43 this. length = 1;
44 this. context = selector;
45 return this;
46}
38th behavior | special use of the operation. It indicates that if selector is not empty, the selector is returned. If selector is empty, the document is returned. If none are empty, returns selector.
Starting from row 41st, the first possibility is to determine whether the parameter object is a DOMElement object. Because the DOMElement object has the nodeType attribute, you can determine whether the parameter is a DOM element.
If yes, create a jQuery object. this indicates the object created by calling the init function in the new method, and then add two members for processing. The first member name is 0, the value is a parameter object, the second member is named length, and the value is 1, which implements an object that imitates an array object. Finally, return this jQuery object.
- 2 pages in total:
- Previous Page
- 1
- 2
- Next Page