Get Method for source code analysis of jQuery1.0 (6)

Source: Internet
Author: User
For ease of writing, jQuery registers on $. In order not to conflict with other frameworks, he first backed up $ to _ $. // Mapoverthe $ incaseofoverwriteif ($) jQuery. _ $; // MapthejQuerynamespacetothe & amp; #39;... SyntaxHighli

 

For ease of writing, jQuery registers on $. In order not to conflict with other frameworks, he first backed up $ to _ $.

 

 

// Map over the $ in case of overwrite

If ($)

JQuery. _ $ = $;

 

// Map the jQuery namespace to the '$' one

Var $ = jQuery;

The following is the jQuery prototype method.

 

 

JQuery. fn = jQuery. prototype = {

Jquery: "$ Rev. 509 $ ";,

 

Size: function (){

Return this. length;

},

 

Get: function (num ){

// Watch for when an array (of elements) is passed in

If (num & num. constructor = Array ){

 

// Use a tricky hack to make the jQuery object

// Look and feel like an array

This. length = 0;

[]. Push. apply (this, num );

Return this;

} Else

Return num = undefined?

 

// Return a 'clean' array

JQuery. map (this, function (a) {return }):

 

// Return just the object

This [num];

},

 

Jquey: indicates the version number. It also indicates whether an object is jquery or not.

Size: returns the size of a jQuery object. A jQuery object is an array-like object with a length value. It can index subscripts, but there is no array method.

 

Get: The get method is flexible and the parameters are optional. If no parameter is specified, a jQuery object array is returned. If the num parameter is set to the numeric type, the num element is returned. If the num parameter is set to the array type, the element of the num type is set to the jQuery object element.

 

 

The highlights of the Code are

 

 

This. length = 0;

[]. Push. apply (this, num );

This indicates the jQuery instance object. Set the attribute length to 0 and press the element in num in the push method.

These two statements are indispensable. After processing is completed, it is an array object of the class, which has the length attribute and can be iterated, but there is no array method.

 

It is equivalent to the following code:

 

 

Var obj = new Object ();

Obj. length = 0;

[]. Push. apply (obj, [1, 2, 3]);

Alert (obj. length); // 3

 

 


Author: baozhifei
Related Article

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.