如何在JavaScript数组再在物理内存中psented $ P $? [英] How are JavaScript arrays represented in physical memory?

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

问题描述

这是我的理解,我可以存储数据好坏参半JavaScript数组中,并改变阵列中的任何元素,一些其他类型。如何跨preTER跟踪任何元素是什么地方在物理内存中。又怎么是数据的,如果我更改为较大的数据类型的元素pvented的下一个元素$ P $改写。

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中,数组是对象类型有特殊的构造函数和存取方法。

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"}]

直接回答你的问题:

Q值。这是我的理解,我可以存储数据好坏参半JavaScript数组中,并改变阵列中的任何元素,一些其他类型。如何跨preTER跟踪任何元素是什么地方在物理内存?此外,如何为数据中,如果我更改为较大的数据类型的元素pvented的下一个元素$ P $改写?

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?

一个。您可能已经知道这一点,如果你读过我的上述评论。在Javascript中,数组是一个Hashtable对象类型,这样间preTER并不需要跟踪的物理内存和改变元素的值不影响其他元素,因为它们不是存储在一个连续的块内存。

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值。我假设数组引用仅存储实际的对象,并放置在阵列时,图元被包裹在幕后。假定是这种情况,如果我对原始变量不同的手柄,改变存储在数组中的值被同步维持?

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. 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.

您可以通过阅读的 ECMA-262规范(版本5.1)

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

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