The "go" JavaScript object uses the array method

Source: Internet
Author: User

Original: http://www.cnblogs.com/idche/archive/2012/03/17/2403894.html

Array.prototype.push

Push adds an entry to the end of the array and updates the length to return the array lengths.

What happens if object uses push?

Look at the code below, and obj works like an array. Length is updated automatically.

var push = Array.prototype.push;
var obj = {};
Push.call (obj, "hello"); return value 1
obj {"0": "hello", length:0}
Push.call (obj, "world"); return value 2
obj {"0": "hello", "1": "world", length:2}


Array.prototype.length Array.prototype.splice

Give the length and splice to Object

Look at the following code: obj, the goods actually become an array? Not actually. this may be a debugging tool for some output check Issues.
We use instanceof to test the obj instanceof Array//false

var obj = {
length:0,
Splice:Array.prototype.splice
};
Console.log (obj); Print: []

Keep looking at the following code:

Obj.push (0)//return obj.length  1
Obj.push (1)//return obj.length 2
Obj.splice (0, 1);//delete The first item to return the delete item [0]
OBJ.LENGTH//1 Splice Update the Length property when deleting an item

This way, the performance of obj is almost the same as the Array. Nothing unexpected slice,pop,shift,unshift anything can work properly in Object.

however, If the length is set directly, the items in the following table with a length greater than "are deleted in the array, but the obj in is not updated."

Where is the application?
The jquery object behaves like an array, in fact he is an object. How is this object new?
The actual jquery lends the array method to object to achieve the effect of this jquery object, and the jquery object also uses a method such as the push array directly Inside.

Look at some of the source code for jquery (note bold )

Start with an empty selector
Selector: "",

The current version of the jQuery being used
Jquery: "1.7.1",

The default length of a JQuery object is 0
length:0,
......

For internal use only.
Behaves like an Array ' s method, not a jQuery method.
push:push,
Sort: [].sort,
Splice: [].splice

If you want to play object as an array, then the potential problem with the length property does not correspond to the sum of the "array" items.

therefore, using length directly will not be Supported.

Look at the jquery code below, Although the length is updated, jquery objects are not updated. (this is not a question of jquery, of Course)

var JQ = $ (' div ')//suppose we get 40 div on the page
Jq.length//40
Jq.length = 0;
jq//? 40 DOM objects are still stored in JQ the length property does not correspond to the sum of the "array" items.



The use of the array method of object can also work properly, it is a bit unexpected, may be practical application far more than These.

The "go" JavaScript object uses the array method

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.