这似乎是一个常见的JavaScript用法:
function foo (array, index) {
if (typeof array[index] == 'undefined')
alert ('out of bounds baby');
}
Run Code Online (Sandbox Code Playgroud)
而不是更普遍(在其他语言中)和概念上更简单:
function foo (array, index) {
if (index >= array.length)
alert ('boo');
}
Run Code Online (Sandbox Code Playgroud)
据我所知,第一种情况也适用于其中有"间隙"的阵列,但这是一个足以保证成语的常见情况吗?
可以在此处看到提示此问题的代码示例.在这种情况下,当在函数内部使用'argument'变量时,假设它是一个连续的数组是不是很明智?