JQuery1.3.2 source code learning 3-4: init Function Analysis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.