This article describes the usage of push method in Javascript array. Share it for your reference, as follows:
Look at the following code:
var o = { 1:'a' ,2:'b' ,length:2 ,push: }; ('c');
Q: What is the internal value now?
My first reaction is rejection. Why do I need to study the behavior of [Explanation Engine] in unreasonable situations? But this kind of inference is sometimes very attractive, so when I came back, I thought about it carefully and found that it was actually very simple.
For push method, what I think of is the stack conditionally reflectively. [Classic Stack of Data Structure] The medium-voltage stack and bullet stack operation are based on the top of the stack pointer. The top of the stack pointer always points to the top of the stack, which means that it will automatically increase or decrease due to the bullet stack. This pointer in an array in javascript is length. So in the above code, ('c') is o.2 = 'c' (of course o.2 cannot be accessed directly, this is just pseudo-code), so the data in o after the code is executed is as follows:
{ 1:'a' ,2:'c' ,length:3 //push operation=>length+1 ,push: }
Additional Notes:
In JavaScript, everything is an object, and JavaScript objects have some differences from 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 all natural numbers). In the above example, the literal value assigned to a actually simulates an array (an array with subscript starting from 1) - of course, there are only some characteristics of the array. For example, when a real array is accessed, it will conduct over-bounds checks based on length.
Just know that the position of push is based on length. The following seemingly strange phenomena are easy to understand:
//Not exists, the engine is set to 0var o = { '1':'a' ,'2':'b' ,push: }; ('c');//c {0:'c',1:'a',2:'b',...} //It is a negative value, which is an interesting question, involving the inverse code and complement code [1]var o = { '1':'a' ,'2':'b' ,length:-1 ,push: }; ('c');//c {1:'a',2:'b',4294967295:'c',length:4294967296,...} // is a character or objectvar o = { 1:'a' ,2:'b' ,length:'A' ,push: }; ('c');//c {0:'c',1:'a',2:'b',length:1,...} I thought the js interpreter would convert A into ASCII code to assign value to length, and finally saw the freedom of javascript but still have moral integrity
The numerical values in the computer are stored in complement. For the sake of easy operation, the complement of -1 is the same as the complement of 4294967295. According to the semantics of length, here is an unsigned number.
[-1]Supplement = 1111 1111 1111 1111 1111 1111 1111 1111 = [4294967295]Supplement = 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 11111 11111
So in this way, we press an object into o in 2, and the key takes 4294967296, but the maximum length limit of the array is 4294967296, which means that the subscript can only get 4294967295, and only 32 bits will be taken - for 4294967296 = 1 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 00000 00000 00000 The last 32 bits will become 0, so the position of push this time is 0.
For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript array operation skills》、《Summary of JavaScript traversal algorithm and skills》、《Summary of JavaScript mathematical operations usage》、《Summary of JavaScript data structure and algorithm techniques》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation effects and techniques"and"Summary of JavaScript Errors and Debugging Skills》
I hope this article will be helpful to everyone's JavaScript programming.