Interesting JavaScript array, javascript Array

Source: Internet
Author: User

Interesting JavaScript array, javascript Array

When we got off work in the evening, we began to discuss boring issues. A colleague proposed the following weird scenario, which is said to be an interview question:

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.

ForPushIn this method, I think of stacks as a condition reflection. [classic stack of data structures] the operation of Medium-pressure stacks and bullet Stacks is based on the top pointer of stacks, and the top pointer of stacks always points to the top of stacks, this means that it will automatically increase or decrease because of the pressure on the stack. In the array in javascript, this pointer isLength.So in the above Code,O. push ('C') is o.2 = 'C' (of course, o.2 cannot be accessed directly, This is just pseudo code ),So the data in o after code execution 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 ).
  • 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.

[1]: The values in the computer are stored in the Complement Method. To facilitate computation, the-1 complement code is the same as the 4294967295 complement code. According to the semantics of lenth, here is the unsigned number [-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.

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.