English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes the usage of the push method in JavaScript arrays. Share it with everyone for reference, as follows:
var o = { 1:'a' ,2:'b' ,length:2 ,push:Array.prototype.push }; o.push('c');
See the following code:
Q: What is the internal value of o now?
For the push method, my first reaction is to reject, why study the behavior of the engine in unreasonable situations? But sometimes this reasoning is very attractive, so when I came back, I carefully thought about it and found it to be very simple.2 = 'c' (of course o.2Cannot be accessed directly, this is just pseudocode), so after the code is executed, the data in o is as follows:
{ 1:'a' ,2:'c' ,length:3 //push operation=>length+1 ,push:Array.prototype.push }
Supplementary explanation:
In JavaScript, everything is an object, and JavaScript objects have some differences from strongly typed objects, which can be understood as a collection of key-value pairs. This is also true for its array type, whose index access is key access (although its keys are all natural numbers). In the above example, the object literal assigned to a actually simulates an array (an array starting from1Starting array) - of course, only some characteristics of the array, such as the real array when performing key access, will check for out-of-bounds based on length.
As long as you know that the position of push is based on length, the following seemingly strange phenomena are understandable:
//1.length does not exist, the engine sets it 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 negative, which is an interesting issue involving original code, complement code, and two's 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 is a character or an 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 JavaScript interpreter would convert A to ASCII code to assign it to length, finally saw the free and decent of JavaScript
Numbers in computers are stored in two's complement form, in order to facilitate computation,-1The two's complement and4294967295The two's complement of the unsigned number is the same, according to the semantics of length, here is an unsigned number
[-1]补 = 1111 1111 1111 1111 1111 1111 1111 1111 = [4294967295补 = 1111 1111 1111 1111 1111 1111 1111 1111
So we get the difference2Press an object into the o in the middle, and the key is taken4294967296, but the maximum length limit of the array is4294967296, that is, the index can only be4294967295, only get32bit——for4294967296 = 1 0000 0000 0000 0000 0000 0000 0000 0000 Take the last32When the position is 0, it becomes 0, so the position pushed this time is 0.
Readers who are interested in more about JavaScript-related content can check out the special topics on this site: 'Summary of JavaScript Array Operation Techniques', 'Summary of JavaScript Traversal Algorithms and Techniques', 'Summary of JavaScript Mathematical Operation Usage', 'Summary of JavaScript Data Structures and Algorithm Techniques', 'Summary of JavaScript Switching Effects and Techniques', 'Summary of JavaScript Search Algorithm Techniques', 'Summary of JavaScript Animation Effects and Techniques', and 'Summary of JavaScript Error and Debugging Techniques'.
I hope the content described in this article will be helpful to everyone in designing JavaScript programs.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the infringing content.)