Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Interesting JavaScript array


May 30, 2021 Article blog



At the end of the evening, we started talking about boredom again. A colleague asked the following bizarre scenario and heard it was an interview question:

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

Colleague: What does O's internal value look like now?

My first reaction was exclusion, so why study the actions of the "explanation engine" in the event of injustice? But this kind of inference is sometimes very attractive, so when I came back, I thought about it and found it very simple.

For push this method, I conditionally reflectively think of the stack, "the classic stack of data structure" in the stack and the stack operation is based on the stack top pointer, the stack top pointer always points to the top of the stack, meaning that it will automatically increase or decrease due to the pressure stack. I n the array in javascript, this pointer is length. So in the code above, o.push ('c') is o.2 s 'c' (although o.2 can't be accessed directly, it's just pseudo-code), so the code fulfills the following data in o:

{1: 'a', 2: 'c', Length: 3 // Push Operation => Length + 1, Push: Array.Prototype.push}

Additional notes:

In JavaScript, everything is an object, and javascript objects are somewhat different from strongly typed objects, and it is understandable that scores are a collection of key-value pairs. Its array type is no exception, its subscript access is key access (although its keys are natural numbers), in the example above assigned to a object literally depicts an array (an array marked from the beginning) - although only some of the characteristics of the array, such as the real array in the key access, according to length cross-border check.

As long as you know that the location of the push is based on length, the following seemingly strange phenomena are well understood:

//1.length does not exist, the engine is set
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, this is an interesting question, hitting the original code back code and 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 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 will convert A to the ASCII code to assign the Length, and finally see the freedom of JavaScript or the exercise.

1: The values in the computer are stored in the form of complements, in order to facilitate operation, (1) complement codes and 4294967295 supplements, according to the semantics of length, here is the number of unsigned

(1)) Supplements : 1111 1111 1111 1111 1111 1111 1111 1111
So we take O in difference pair 2 into an object, key takes 4294967296, but the maximum length limit of the array is 4294967296, that is, the bid can only be taken to 4294967295, only to 32 bits - pair 4294967296 - 1 0000 0000 0000 0000 0000 0000 0000 000000 Takes the last 32 bits and becomes 0, so this push position is 0.