JavaScript Object array method using detailed _javascript techniques

Source: Internet
Author: User
Array.prototype.push
Push adds an item to the tail of the array and updates the length to return the array lengths.
What happens if object uses push?
Looking at the code below, obj works like an array. Length is automatically updated.
Copy Code code 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 Object
Look at the following code: OBJ, this product has become an array? It's not. This may be some output checking problem with the debugging tools.
We used 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
1obj.push (1)//return Obj.length
2obj.splice (0, 1);//delete first item return delete item
[0]obj.length//1 Splice also update the length property when deleting an item
So obj behaves almost like an array. Nothing unexpected slice,pop,shift,unshift or anything can work in the object.
However, if you set length directly, the items in the following table that are larger than length 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 does this object get new out?
Real jquery lends the array's method to object to achieve the effect of this jquery object, and the JQuery object also uses the push-and-array method directly inside.
Take a look at some of jquery's source code (note bold)
Copy Code code 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.
Behaves like a Array of ' s method, not like a jQuery method.
Push:push,
Sort: [].sort,
Splice: [].splice

If you want to play an array with object, the potential problem length property does not correspond to the sum of the "array" items.
Therefore, it is not supported to use the length setting directly.

Look at the jquery code below, and although the length is updated, the jquery object hasn't been updated. (This is not a question of jquery, of course)
var JQ = $ (' div ')//Suppose we get 40 on the page
Divjq.length//40
Jq.length = 0;jq//? The JQ still holds 40 Dom object length properties that do not correspond to the sum of the "array" items.
Object using array method can still work, it is a bit unexpected, may be practical applications more than these.

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.