JavaScript 数组如何在物理内存中表示? [英] How are JavaScript arrays represented in physical memory?

查看:23
本文介绍了JavaScript 数组如何在物理内存中表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,我可以将混合数据存储在 JavaScript 数组中,也可以将数组中的任何元素更改为其他类型.解释器如何跟踪任何元素在物理内存中的位置.如果我将元素更改为更大的数据类型,如何防止下一个元素中的数据被覆盖.

It is my understanding that I can store mixed data in a JavaScript array, as well as change any element in the array to some other type. How does the interpreter keep track of what place in physical memory any element is at. Also how is the overwriting of the data in the next element prevented if I change an element to a larger data type.

我假设数组只存储对实际对象的引用,当放置在数组中时,基元被包装在幕后.

I assume that arrays only store references to actual objects, and primitives are wrapped behind the scenes when placed in arrays.

假设是这种情况,如果我对原始变量有不同的句柄并更改存储在数组中的值是否保持同步?

Assuming this is the case, If I have a different handle on the primitive variable and change the value stored in the array is synchronicity maintained?

我知道我可能已经回答了我自己的问题,但我不确定,我也找不到关于此事的任何信息.

I know I probably already answered my own question, but I don't know for sure and I can't find any information on the matter.

推荐答案

通常,数组分配固定长度的连续内存块.但是,在 Javascript 中,数组是具有特殊构造函数和访问器方法的 Object 类型.

Normally, arrays allocate a contiguous block of memory of fixed length. However, in Javascript, arrays are Object types with special constructors and accessor methods.

这意味着,像这样的语句:

Which means, a statement like:

var arr = new Array(100000);

不分配任何内存!实际上,它只是设置数组中的长度属性的值.构造数组时,不需要声明大小,因为它们会自动增长.所以,你应该改用这个:

does not allocate any memory! In fact, it simply sets the value of the length property in the array. When you construct an array, you don't need to declare a size as they grow automatically. So, you should use this instead:

var arr = [];

Javascript 中的数组是稀疏的,这意味着并非数组中的所有元素都可能包含数据.换句话说,只有实际包含数据的元素存在于数组中.这减少了阵列使用的内存量.这些值由键而不是偏移量定位.它们只是一种方便的方法,并不打算用于复杂的数值分析.

Arrays in Javascript are sparse which means not all the elements in the array may contain data. In other words, only the elements that actually contain data exist in the array. This reduces the amount of memory used by the array. The values are located by a key and not by an offset. They're simply a method of convenience and not intended to be used for complex numerical analysis.

Javascript 中的数组没有类型化,因此元素的值可以是对象、字符串、数字、布尔值、函数或数组.数组和对象之间的主要区别在于长度属性的值大于数组中最大的整数键.

Arrays in Javascript are not typed so the value of an element can be an object, string, number, boolean, function or an array. The main difference between an array and an object is the length property which has a value greater than the largest integer key in the array.

例如:

您可以创建一个空数组,并在索引 0 和索引 99 处添加两个元素.长度为 100,但数组中的元素数为 2.

You could have an create an empty array and add two elements at index 0 and index 99. The length would be 100, but the number of elements in the array would be 2.

var arr = [];
arr[0] = 0;
arr[99] = {name: "John"};
console.log(arr.length); // prints 100
arr; // prints something like [0, undefined × 98, Object { name: "John"}]

直接回答您的问题:

问.我的理解是,我可以将混合数据存储在 JavaScript 数组中,也可以将数组中的任何元素更改为其他类型.解释器如何跟踪任何元素在物理内存中的位置?另外,如果我将元素更改为更大的数据类型,如何防止下一个元素中的数据被覆盖?

Q. It is my understanding that I can store mixed data in a JavaScript array, as well as change any element in the array to some other type. How does the interpreter keep track of what place in physical memory any element is at? Also, how is the overwriting of the data in the next element prevented if I change an element to a larger data type?

A.如果您已经阅读了我上面的评论,您现在可能已经知道这一点.在 Javascript 中,数组是 Hashtable 对象类型,因此解释器不需要跟踪物理内存,并且更改元素的值不会影响其他元素,因为它们没有存储在连续的内存块中.

A. You probably know this by now if you've read my comments above. In Javascript, an array is a Hashtable Object type so the interpreter doesn't need to keep track of physical memory and changing the value of an element doesn't affect other elements as they're not stored in a contiguous block of memory.

--

问.我假设数组只存储对实际对象的引用,当放置在数组中时,基元被包装在幕后.假设是这种情况,如果我对原始变量有不同的句柄并更改存储在数组中的值是否保持同步?

Q. I assume that arrays only store references to actual objects, and primitives are wrapped behind the scenes when placed in arrays. Assuming this is the case, if I have a different handle on the primitive variable and change the value stored in the array is synchronicity maintained?

A.不,原语没有被包装.更改分配给数组的原语不会更改数组中的值,因为它们是按值存储的.另一方面,对象是通过引用存储的,因此更改对象值将反映该数组中的更改.

A. No, primitives are not wrapped. Changing a primitive that was assigned to an array will not change the value in the array as they're stored by value. Objects on the other hand are stored by reference, so changing the objects value will reflect that change in that array.

以下是您可以尝试的示例:

var arr = [];
var obj = { name: "John" };
var isBool = true;

arr.push(obj);
arr[1] = isBool;

console.log(arr[0]); // print obj.name
console.log(arr[1]); // print true

obj.age = 40;        // add age to obj
isBool = false;      // change value for isBool

console.log(arr[0]); // value here will contain age
console.log(arr[1]); // value here will still be true

另外,请注意,当您通过以下两种方式初始化数组时,它具有不同的行为:

Also, note that when you initialize an array in the following two ways, it has a different behavior:

var arr = new Array(100);
console.log(arr.length);        // prints 100
console.log(arr);               // prints []

var arr2 = new Array(100, 200);
console.log(arr2.length);       // prints 2
console.log(arr2);              // prints [100, 200]

如果您想使用 Javascript 数组作为连续的内存块,您应该考虑使用 TypedArray.TypedArray 允许您将内存块分配为字节数组并更有效地访问原始二进制数据.

If you want to use Javascript Arrays as contiguous blocks of memory, you should look into using TypedArray. TypedArray's allow you to allocate a block of memory as a byte array and access the raw binary data more efficiently.

您可以通过阅读了解有关 Javascript 复杂性的更多信息ECMA-262 规范(5.1 版).

这篇关于JavaScript 数组如何在物理内存中表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆