[英]How does Array.push() method work exactly in Javascript?
while I was taking online CS class, the topic was about an overview of how Array and memory works, and the teacher used C as an Example as that you cannot simply add extra element to an existing Array while the Array is already full with element.当我在线学习 CS class 时,主题是关于 Array 和 memory 工作原理的概述,老师使用 C 作为示例,因为您不能简单地添加额外的元素As a beginner developer who started with JavaScript, even though I know JavaScript is a high level language and Array.push() is already a familiar function to me, but it seems like it doesn't fit such context, out of curiosity, I've search through google and StackOverflow, I just don't see people discussing why JavaScript can just add extra elements to an existing Array.
作为一个从 JavaScript 开始的初学者开发人员,尽管我知道 JavaScript 是一种高级语言,并且 Array.push() 已经是一种熟悉的 ZC1C425268E68385D1AB5074C17A94F14,但我似乎不适合这种情况,但它似乎不适合我的上下文已经通过谷歌和 StackOverflow 搜索,我只是没有看到人们讨论为什么 JavaScript 可以只向现有数组添加额外的元素。
Does JavaScript simply create a new Array with added element and point the variable I've already assigned to to the new Array or something else? JavaScript 是否只是简单地创建一个带有添加元素的新数组并将我已经分配给新数组的变量指向新数组或其他东西?
A JavaScript Array
is not like a C array, it's more like a C++ std::vector
. JavaScript
Array
不像 C 数组,它更像是 C++ std::vector
。 It grows in length by dynamically allocating memory when needed.它通过在需要时动态分配 memory 来增加长度。
As stated in the reference documentation for std::vector
:如
std::vector
的参考文档中所述:
Vectors usually occupy more space than static arrays, because more memory is allocated to handle future growth.
向量通常比 static arrays 占用更多的空间,因为分配了更多的 memory 来处理未来的增长。 This way a vector does not need to reallocate each time an element is inserted, but only when the additional memory is exhausted.
这样,向量不需要在每次插入元素时重新分配,而仅在额外的 memory 用尽时才需要重新分配。 [...] Reallocations are usually costly operations in terms of performance.
[...] 就性能而言,重新分配通常是代价高昂的操作。
Arrays in javascript are not the same as an array in C. javascript 中的 Arrays 与 C 中的数组不同。 In C you'll allocate a new array with type and a fixed size and if you want to alter the size you'll have to create a new one.
在 C 中,您将分配一个具有类型和固定大小的新数组,如果您想更改大小,则必须创建一个新数组。 For javascript arrays on the other hand if you have a look at the description of arrays over at MDN you'll see this line.
另一方面,对于 javascript arrays 如果您在 MDN 上查看 arrays的描述,您会看到这一行。
Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations.
Arrays 是类列表对象,其原型具有执行遍历和变异操作的方法。 Neither the length of a JavaScript array nor the types of its elements are fixed.
JavaScript 数组的长度及其元素的类型都不是固定的。 Since an array's length can change at any time, and data can be stored at non-contiguous locations in the array
由于数组的长度可以随时更改,并且数据可以存储在数组中的非连续位置
So while it is called "array" it is more like a list which lets you resize it freely (and also store anything, because javascript).因此,虽然它被称为“数组”,但它更像是一个列表,可以让您自由调整它的大小(并且还可以存储任何东西,因为 javascript)。
There is also another question about the semantics of the push/pop methods here on SO that can shine some more light on those: How does the Javascript Array Push code work internally还有另一个关于 SO 上的 push/pop 方法的语义的问题,可以更清楚地说明这些问题: Javascript Array Push 代码如何在内部工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.