This example describes the use of push methods in JavaScript arrays. Share to everyone for your reference, specific as follows:
Look at the following code:
var o = {
1: ' A '
, 2: ' B '
, Length:2
, Push:Array.prototype.push
};
O.push (' C ');
Q:o what is the internal value now?
My first reaction is rejection, why study the behavior of "interpreting engine" under unreasonable circumstances? But this inference is sometimes fascinating, so when I came back I thought it over and found it was really simple.
To push this method, I am conditioned to think of the stack, "Classic data structure stack" in the pressure stack and stack operation based on the top of the stack pointer, stack top pointer always point to the top of the stack, meaning that it will automatically increase or decrease because of the compression stack. In a JavaScript array, this pointer is length. So in the above code, O.push (' C ') is o.2 = ' C ' (of course o.2 can't be accessed directly, this is just pseudocode), so the data in the O is as follows:
{
1: ' A '
, 2: ' C '
, Length:3//push operation =>length+1
, Push:Array.prototype.push
}
Supplementary Note:
In JavaScript, everything is object, and JavaScript objects have different places with strongly typed objects, which can be understood as a set of key-value pairs. Its array type is no exception, its subscript access is key access (but its keys are natural numbers), the object literal that is assigned to a in the above example actually simulates an array (an array of subscripts starting at 1)--of course, only a subset of the properties of the array, such as a real array, when the key is accessed, will be checked for boundaries based on length.
As long as you know the position of the push is based on length, all of the following seemingly strange phenomena are well understood:
1.length does not exist, engine is set to 0
var o = {
' 1 ': ' A '
, ' 2 ': ' B ',
Push:Array.prototype.push
};
O.push (' C ');//c {0: ' C ', 1: ' A ', 2: ' B ',...}
2.length is a negative value, which is an interesting issue, involving the original code and the complement "1"
var o = {
' 1 ': ' A '
, ' 2 ': ' B ',
length:-1
, push: Array.prototype.push
};
O.push (' C ');//c {1: ' A ', 2: ' B ', 4294967295: ' C ', length:4294967296,...}
3.length for character or object
var o = {
1: ' A '
, 2: ' B '
, Length: ' A '
, Push:Array.prototype.push
};
O.push (' C ');//c {0: ' C ', 1: ' A ', 2: ' B ', Length:1,...} I thought the JS interpreter would convert a to ASCII to assign the length, and finally see the freedom of JavaScript or the moral.
The value of the computer is stored in the complement, in order to facilitate the operation,-1 of the complement and 4294967295 complement, according to the semantics of length, here is unsigned number
[-1] complement = 1111 1111 1111 1111 1111 1111 1111 1111 = [4294967295] complement = 1111 1111 1111 1111 1111 1111 1111 1111
So we're going to push the O in 2 to an object, the key takes 4294967296, but the maximum length of the array is limited to 4294967296, which means that the subscript can only be taken to 4294967295, only to 32--For 4294967296 = 1 0000 The position of the push is 0000, with 0000 0000 0000 0000, 0000 0000 32 and 0 digits.
More readers interested in JavaScript-related content can view the site topics: "javascript array Operation tips Summary", "JavaScript traversal algorithm and Skills summary", "JavaScript Mathematical calculation Usage Summary", " JavaScript data structure and algorithm skills summary, "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and techniques summary" and "JavaScript error and debugging skills Summary"
I hope this article will help you with JavaScript programming.