使用JavaScript新的Array(n)声明 [英] Use of JavaScript new Array(n) Declaration

查看:151
本文介绍了使用JavaScript新的Array(n)声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本的JavaScript问题:由于Java的情况没有硬限制(例如 IndexOutOfBoundsException ),我们指定长度属性的声明的用途是什么?

Basic JavaScript question: Since there is no hard limit for arrays as the case with Java (i.e. IndexOutOfBoundsException), what is the use of the declaration where we specify the length property?

var a = new Array(10);

我知道它预定了长度并将未定义放入这些空白点。这个理由足以让它拥有吗?

I know it predefines the length and puts "undefined" into those empty spots. Is that reason enough for having it?

推荐答案

声明数组大小有许多明显的好处,但我认为大多数感知到的好处只是传递FUD。

There are many perceived benefits of declaring an array size, but I think the majority of the perceived benefits are just FUD being passed around.

更好的表现!/它更快!

据我所知,预分配和动态分配之间的区别可以忽略不计。

As far as I can tell the difference between pre-allocating and dynamic allocation is negligible.

更有趣的是,规范声明数组应该设置为预先分配的长度!

More interestingly, the spec does not state that the array should be set to a pre-allocated length!

来自第15.4.2.2节 ECMA-262

From Section 15.4.2.2 ECMA-262:


如果参数 len 是一个数字,ToUint32( len )等于 len ,然后是新构造的对象的长度属性设置为ToUint32( len )。如果参数 len 是一个数字且ToUint32( len )不等于 len ,则 RangeError 异常是抛出。

If the argument len is a Number and ToUint32(len) is equal to len, then the length property of the newly constructed object is set to ToUint32(len). If the argument len is a Number and ToUint32(len) is not equal to len, a RangeError exception is thrown.

这里有一个不科学的有趣测试案例: http://jsbin.com/izini

An unscientific for-fun test case is here: http://jsbin.com/izini

这使代码更容易理解!

就我个人而言,我不同意。

Personally, I disagree.

考虑你过去编写的javascript,并考虑你可能需要的代码写在将来。我无法想到我需要在其中一个数组上指定静态限制的时间。我还认为,在javascript中限制数组的潜在问题远远超过让人们知道你在想什么而没有实际检查所带来的好处。让我们权衡利弊......

Consider the javascript you have written in the past, and consider code you may have to write in the future. I can't think of a single time where I've needed to specify a static limit on one of my arrays. I'd also argue that the potential problems of limiting arrays in javascript highly outweigh the benefits caused by letting people know what you were thinking with no actual checks behind it. Lets weigh the pros and cons...

优点:


  1. 它会他们更容易理解你的代码要做什么。

  2. 他们将能够找到你后来的假设引起的错误(舌头坚定地在脸颊上)

缺点:


  1. 快速浏览很容易混淆新阵列(10)withnew Array('10')做完全不同的事情!

  2. 你对代码施加了一个任意限制,没有正常的长度限制导致你写了很多用于检查和维持限制的锅炉板代码。

  3. 您对代码施加了任意限制,这可能已被推广为使用任意长度的值。

  4. 你假设人们会如何阅读你的代码,同时假设替代方案不那么混乱。

  1. Quick glances can easily confuse "new Array(10)" with "new Array('10')" which do entirely different things!
  2. You are imposing an arbitrary limit on code with no normal length limit causing you to write lots of boiler plate code to check and maintain the limit.
  3. You are imposing an arbitrary limit on code which could probably have been generalized to work with any length of values.
  4. You're making an assumption about how people will read your code while assuming that the alternative would be less confusing.

您可以写一下:

//I assume this array will always be length 10
var arr = new Array();

在上述情况下,评论甚至可能更可取。明确的意图声明可以避免任何混淆,不习惯使用构造函数作为意图声明。

In the above case the comment might even be preferable. The explicit declaration of intent can avoid any confusion not used to using the constructor as a declaration of intent.

很好然后..为什么你认为它甚至存在那么?

方便。当他们编写规范时,我认为他们实现了两件事。

Convenience. When they were writing the spec I think they realized two things.


  1. 这种分配将是使用来自类似语言的开发人员的东西来自。

  2. ECMAScript的实现可能会使用它来提高性能。

所以他们放了它在那里。
规范仅定义参数的使用,而不是如何实现。

So they put it in there. The spec only defines the use of the parameter, not how it should be implemented.

这篇关于使用JavaScript新的Array(n)声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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