How to Use the array method of javascript object

Source: Internet
Author: User

Array. prototype. push
Push adds an item to the end of the array and updates the length to return the array length.
What if the Object uses push?
Look at the following code. obj works like an array. Length is automatically updated.
Copy codeThe Code is as follows:
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 length and splice to the Object
See the following code: Is obj actually an array? In fact, this may be some output check Problems of the debugging tool.
We use instanceof to test obj instanceof Array // false
Var obj = {length: 0, splice: Array. prototype. splice}; console. log (obj); // print: []

Continue to read the following code:
Obj. push (0) // returns obj. length
1obj. push (1) // return obj. length
2obj. splice (0, 1); // Delete the first item and return the delete item.
[0] obj. length // 1 The length attribute is also updated when splice deletes an item.
In this way, the expression of obj is almost the same as that of array. Slice, pop, shift, and unshift can all work normally in the object.
However, if you set the length directly, the entries in the following Table greater than the length will be deleted in the array, but the obj in will not be updated.

Where is the application?
The jQuery object is represented as an array, which is actually an object. How to create new objects?
Actually, jQuery lent the Array method to the Object to achieve the effect of this jQuery Object. The jQuery Object also directly uses the Array method such as push.
Look at some of jQuery's source code (bold)
Copy codeThe Code is as follows:
// Start with an empty selector
Selector :"",
// The current version of 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 like a jQuery method.
Push: push,
Sort: []. sort,
Splice: []. splice

If you want to play an Object as an Array, the length attribute may not correspond to the sum of the "Array" items.
Therefore, setting the length directly using length is not supported.

Check the jquery code below. Although the length is updated, the jquery object is not updated. (Of course this is not a problem with jquery)
Var jq = $ ('div ') // assume that we have obtained 40
Divjq. length // 40
Jq. length = 0; jq //? The length attribute of 40 dom objects in jq does not match the sum of the "array" items.
The array method can still work normally for objects, which is unexpected. It may be far more practical than this.

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.