Analysis of push method usage in Javascript Array

Source: Internet
Author: User
Tags javascript array
The example in this article describes how to use the push method in the Javascript array. We will share this with you for your reference. The details are as follows:

See the following code:

var o = {  1:'a'  ,2:'b'  ,length:2  ,push:Array.prototype.push};o.push('c');

Q: What is the current internal o value?

My first reaction was rejection. Why do I need to study the [interpretation engine] behavior in an unreasonable situation? However, this inference is sometimes very attractive, so when I came back, I thought carefully and found that it was actually very simple.

For the push method, I think of the stack as a condition reflection. [classic stack of Data Structure] the operation of the Medium-pressure stack and the elastic stack is based on the top pointer of the stack, the top pointer of the stack always points to the top of the stack, which means it will automatically increase or decrease due to the pressure on the stack. In the array in javascript, the pointer is length. So in the above Code, o. push ('C') is o.2 = 'C' (of course o.2 cannot be accessed directly, this is only 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: Array. prototype. push}

Note:

In JavaScript, all objects are objects, while javascript objects are different from strong objects, which can be understood as a set of key-value pairs. Its array type is no exception. Its subscript access is key access (however, its keys are all natural numbers ), in the above example, the literal volume of the object assigned to a actually simulates an array (an array whose subscript starts from 1)-of course, only some of the array features, for example, when the input key is used to access the real array, the system checks the cross-border according to the length.

As long as you know that the push position is based on length, you can understand the following seemingly strange phenomena:

// 1. length does not exist. The engine is set to 0var 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. This is an interesting issue. It involves the back code of the original code and the complement code [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 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 that the js interpreter would convert A to an ASCII code to assign A value to length. Finally, I saw that javascript is free and it is still feasible.

The values in the computer are stored in the Complement Method. For the convenience of calculation, the-1 complement code is the same as the 4294967295 complement code. According to the length semantics, here is the number of unsigned characters.

[-1] fill = 1111 1111 1111 1111 1111 1111 1111 1111 = [4294967295] fill = 1111 1111 1111 1111 1111 1111 1111 1111

In this case, we press o in 2 into an object, and the key is 4294967296, but the maximum length of the array is 4294967296, that is, the subscript can only be obtained to 4294967295, only 32 bits will be obtained. For 4294967296 = 1 0000 0000 0000 0000 0000 0000 0000 0000, the last 32 bits will be taken. Therefore, the push position is 0.

Related Article

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.