An array in JavaScript is really just a special type of object.
(JavaScript中的数组实际上只是一种特殊的对象类型。)
As such, the "indexes" of the array are really just properties storing an integer value, and the data being stored are just pointers to the allocated memory blocks-- very much like a linked list.(这样,数组的“索引”实际上只是存储整数值的属性,而存储的数据只是指向已分配存储块的指针,这非常像一个链表。)
This is why you can use array methods like push or pop on the existing array without re-allocating the block of memory set aside for the array.
(这就是为什么您可以在现有数组上使用诸如push或pop之类的数组方法而无需重新分配为该数组预留的内存块的原因。)
I don't know the exact details of what V8 is doing, but I'm going to assume because of JavaScript's loosely/dynamically typed nature, it's probably not allocating memory contiguously like you used in your example-- there are just too many potential drawbacks to that for such a weak and dynamically typed language.
(我不知道V8在做什么的确切细节,但是由于JavaScript的松散/动态类型化性质,我将假设它可能不会像您在示例中所使用的那样连续分配内存-潜力太多了这种弱且动态键入的语言的缺点。)
JavaScript basically makes everything an object.
(JavaScript基本上使一切成为对象。)
An array is an object, the indexe values of the array, just pointers to objects.(数组是一个对象,该数组的索引值只是对象的指针。)
Which is why every data type is a variable and has access to properties and methods.(这就是为什么每种数据类型都是变量并且可以访问属性和方法的原因。)
In reality, the address spaces of:
(实际上,以下地址空间:)
let a = [1, 2, 3, 4, 5];
Would be pointers to the allocated memory blocks such as 548995, 48885, 3889282, 093838, 7883344. Or something like that.
(指向分配的内存块的指针,例如548995、48885、3888892、093838、7883344。或类似的东西。)
When you re-allocate any of them, JavaScript will find a block of memory in the heap and set the array index value to the allocated block's pointer.(当您重新分配它们中的任何一个时,JavaScript将在堆中找到一个内存块,并将数组索引值设置为所分配的块的指针。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…