仅通过添加元素而不推送 javascript 来更改数组大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33903514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:01:36  来源:igfitidea点击:

Change array size by just adding element and no push javascript

javascriptangularjs

提问by PTN

I know the universal way of changing an array's size is to use .push().

我知道更改数组大小的通用方法是使用.push().

However, today I saw a piece of code in angularJS that does something like this:

然而,今天我在 angularJS 中看到了一段代码,它做了这样的事情:

var service = {
   pages: [],
   doSmth: doSmth
};

doSmth();

function doSmth() {
   service.pages[1] = "abc";
   service.pages[5] = "def";
}

I ran the debugger on the browser and found that before doSmth()is called, pages[1]is undefined, but after that, pages[1]is assigned the value without any error.

我在浏览器上运行调试器,发现 beforedoSmth()被调用,pages[1]是未定义的,但之后,pages[1]被分配的值没有任何错误。

How is this possible?

这怎么可能?

回答by Brennan

That's just the magic that JavaScript allows. If you come from a language like Java or C, this may seem like a weird idea, but you can set the value of any index in the array at any time, and the array will expand to that size!

这就是 JavaScript 所允许的魔法。如果您来自 Java 或 C 之类的语言,这似乎是一个奇怪的想法,但是您可以随时设置数组中任何索引的值,数组将扩展到该大小!

Consider:

考虑:

var t = [];
t.length === 0;
t[10000] = 'value';
t.length === 10001;

JavaScript just handles this behind the scenes. It's worth mentioning that this behavior is not specific to JavaScript. This is seen a bit in other interpreted languages as well. Ruby, for example, allows you to do the same.

JavaScript 只是在幕后处理这个。值得一提的是,这种行为并不是 JavaScript 特有的。这在其他解释性语言中也有一些。例如,Ruby 允许您执行相同的操作。

Additionally in JavaScript, lengthis a writeable attribute of arrays, so you can easily truncate or clear an entire array:

此外,在 JavaScript 中,length是数组的可写属性,因此您可以轻松截断或清除整个数组:

var t = [1];
t[4] = 0;
t === [1, undefined, undefined, undefined, 0];
t.length = 2;
t === [1, undefined];
t.length = 0;
t === [];

Setting the length to 0 is one of the fastest and simplest ways to clear an array. It might not be the most intuitive solution, but it's my go-to.

将长度设置为 0 是清除数组的最快和最简单的方法之一。它可能不是最直观的解决方案,但它是我的首选。

回答by TbWill4321

An array in JavaScript is just an object with some special properties. Essentially, they are just objects with positive, integer property names. There are some other key differences, but the idea is that you could do this:

JavaScript 中的数组只是具有一些特殊属性的对象。本质上,它们只是具有正整数属性名称的对象。还有一些其他主要区别,但我们的想法是您可以这样做:

var obj = { };
obj['1'] = 'abc';

So you can do the same with an array.

所以你可以对数组做同样的事情。

However! You shouldn't. Modern JavaScript engines usually optimize arrays by backing them with fast, native implementations. By setting indexes that are not currently allocated will de-optimize your code into something more like an object, which is much slower.

然而!你不应该。现代 JavaScript 引擎通常通过使用快速的本地实现来支持数组来优化数组。通过设置当前未分配的索引会将您的代码优化为更像一个对象的东西,这会慢得多。

回答by CoderPi

Pages in function doSmth()is not in the same namespace as pages in service, wich is service.pages.

Pages infunction doSmth()与 pages in 不在同一个命名空间中service,而service.pages.

Your pages[1]musst be a global declared somewhere else.

pages[1]必须是在其他地方声明的全局变量。

However you are initializing pagesas an empty (but existing) array: pages: [],

但是,您正在初始化pages为一个空的(但存在的)数组:pages: [],

var service = {
    pages: [],
    doSmth: doSmth
};

doSmth();

function doSmth() {
    pages[1] = "abc";
    pages[5] = "def";
}