Home  >  Article  >  Web Front-end  >  Detailed explanation of the use of javascript object array method_javascript skills

Detailed explanation of the use of javascript object array method_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:47:31928browse
Array.prototype.push
push adds an item to the end of the array and updates length, returning the array length.
What happens if Object uses push?
Look at the code below, obj works like an array. length will be updated automatically.
Copy code The 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 Object
Look at the following code: Obj actually becomes an array? In fact, otherwise this may be some output checking problem 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 look at the following code:
obj.push(0)//Return obj.length
1obj.push(1)//Return obj.length
2obj.splice (0, 1);//Delete the first item and return the deleted item
[0]obj.length // 1 splice also updates the length attribute
when deleting an item, so that the performance of obj is almost the same as array. Not surprisingly, slice, pop, shift, unshift, etc. can all work normally in object.
However, if length is set directly, the items in the table below that are greater than length will be deleted from the array, but the obj inside will not be updated.

Where is it applied?
The jQuery object behaves like an array, but in fact it is an object. How to new this kind of object?
In fact, jQuery lends Array methods to Object to achieve the effect of this jQuery object. Array methods such as push are also directly used inside jQuery objects.
Look at part of the source code of jQuery (note the bold)
Copy the code The 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 Object as Array, then there may be a potential problem that the length attribute will not Corresponds to the sum of the "array" items.
So directly using length to set the length will not be supported.

Look at 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') //Suppose we obtain 40
divjq.length on the page // 40
jq.length = 0;jq //? There are still 40 dom objects stored in jq, and the length attribute does not correspond to the sum of the "array" items.
Object's method of using array can still work normally, which is a bit unexpected. The actual application may be far more than this.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn