JavaScript 中的“未定义 x 1”是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10683773/
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-26 10:41:04  来源:igfitidea点击:

What is "undefined x 1" in JavaScript?

javascriptarraysooparguments

提问by benqus

I'm doing some small experiments based on this blog entry.

我正在根据这个博客条目做一些小实验。

I am doing this research in Google Chrome's debugger and here comes the hard part.

我正在 Google Chrome 的调试器中进行这项研究,这里是困难的部分。

What the heck is this?!

这是什么鬼?!

I get the fact that I can't delete local variables (since they are not object attributes). I get that I can 'read out' all of the parameters passed to a function from the array called 'arguments'. I even get it that I can't delete and array's element, only achieve to have array[0]have a value of undefined.

我知道我无法删除局部变量(因为它们不是对象属性)。我知道我可以从名为“arguments”的数组中“读出”传递给函数的所有参数。我什至知道我不能删除和数组的元素,只能实现具有array[0]未定义的值。

Can somebody explain to me what undefined x 1means on the embedded image?

有人可以向我解释undefined x 1嵌入图像的含义吗?

And when I overwrite the function footo return the arguments[0], then I get the usual and 'normal' undefined.

当我覆盖函数foo以返回 时arguments[0],我会得到通常和“正常”的未定义。

This is only an experiment, but seems interresting, does anybody know what undefined x 1refers to?

这只是一个实验,但似乎很有趣,有人知道undefined x 1指的是什么吗?

回答by Felix Kling

That seems to be Chrome's new way of displaying uninitialized indexes in arrays (and array-likeobjects):

这似乎是 Chrome 在数组(和类似数组的对象)中显示未初始化索引的新方法:

> Array(100)
[undefined × 100]

Which is certainly better than printing [undefined, undefined, undefined,...]or however it was before.

这当然比印刷[undefined, undefined, undefined,...]或以前更好。

Although, if there is only one undefinedvalue, they could drop the x 1.

虽然,如果只有一个undefined值,他们可能会删除x 1.

回答by Slai

Newer versions of Chrome use emptyinstead of undefinedto make the difference a bit clearer.

较新版本的 Chrome 使用empty而不是undefined使差异更清晰。

For example, entering [,,undefined,,]in the DevTools console results in:

例如,[,,undefined,,]在 DevTools 控制台中输入会导致:

▼(4) [empty × 2, undefined, empty]
   2: undefined
   length: 4
  ?__proto__: Array(0)

回答by nonopolarity

Google Chrome seems to choose to display sparse arrayusing this undefined x nnotation. It will show [undefined, undefined]if it is not a sparse array:

谷歌浏览器似乎选择使用这种表示法显示稀疏数组undefined x n。它将显示[undefined, undefined]它是否不是稀疏数组:

var arr = new Array(2);
console.log(arr);

var arr2 = [];
arr2[3] = 123;
console.log(arr2);

var arr3 = [,,,];
console.log(arr3)

var arr4 = [,];
console.log(arr4)

var arr5 = [undefined, undefined]
console.log(arr5)

var arr6 = [undefined]
console.log(arr6)

arr1 to arr4 are all sparse arrays, while arr5 and arr6 are not. Chrome will show them as:

arr1 到 arr4 都是稀疏数组,而 arr5 和 arr6 不是。Chrome 会将它们显示为:

[undefined × 2] 
[undefined × 3, 123] 
[undefined × 3] 
[undefined × 1] 
[undefined, undefined] 
[undefined] 

note the [undefined x 1]for the sparse array.

注意[undefined x 1]稀疏数组的 。

Since you deleted an element, it follows that: If you delete an element from an array, the array becomes sparse.

由于您删除了一个元素,因此:如果您从数组中删除一个元素,则该数组将变得稀疏。

回答by Ankur Marwaha

Here is how it looks, Type this code into chrome devtools console:-

这是它的外观,将此代码输入 chrome devtools 控制台:-

arr = new Array(4);
arr[2] = "adf";
console.log(arr);    // [undefined × 2, "adf", undefined × 1]

See the first two consecutive "undefined" that are represented by *2, to show that there are two consecutive undefined there

查看*2所代表的前两个连续的“undefined”,说明那里有两个连续的undefined

回答by Firsh - LetsWP.io

It just happened to me too. Open the same thing in another browser and output/log the array to the console. As long as it works in both crome and in firefox the same way (as in accessing the elements works fine even if the output has the undefinedx1), I consider it some kind of bug in Chrome not refreshing something internally. You can actually test it this way:

它也发生在我身上。在另一个浏览器中打开相同的内容并将数组输出/记录到控制台。只要它在crome和firefox中都以相同的方式工作(因为即使输出具有undefinedx1,访问元素也能正常工作),我认为它是Chrome中的某种错误,无法在内部刷新某些内容。您实际上可以通过以下方式对其进行测试:

  1. I was adding elements to an array, and firing console.log() to log the element, to check if it was undefined or not. They were all defined.
  2. After push() I logged the array and it seemed that when this happened, it was always the last one being undefined x 1 on Chrome.
  3. Then I tried logging it more times and also later, but with the same result.
  4. However when I accessed (for the sake of output), the element by its index, it returned the proper value (funny).
  5. After that I logged the array again and it said the last index was undefined x 1 (the very one I just accessed directly with success!).
  6. Then I did a for loop on the array, logging or using each element, all showed up fine.
  7. Then another log on the array, still buggy.

    The code I'm originally using has checks for undefined and they didn't fire, but I was seeing all these undefineds in the console and it was bugging me (no pun intended).

  1. 我正在向数组中添加元素,并触发 console.log() 来记录元素,以检查它是否未定义。他们都被定义了。
  2. 在 push() 之后,我记录了数组,似乎发生这种情况时,Chrome 上总是最后一个 undefined x 1。
  3. 然后我尝试记录它更多次和以后,但结果相同。
  4. 但是,当我访问(为了输出)元素时,它的索引返回了正确的值(有趣)。
  5. 之后,我再次记录了数组,它说最后一个索引是 undefined x 1(我刚刚成功直接访问的那个!)。
  6. 然后我在数组上做了一个 for 循环,记录或使用每个元素,一切都很好。
  7. 然后再次登录阵列,仍然有问题。

    我最初使用的代码检查了 undefined 并且它们没有触发,但是我在控制台中看到了所有这些 un​​defined 并且它正在困扰我(没有双关语)。

Also worth reading: Unitialized variables in an array when using only Array.push?I'm using pop() too in my code and it's a reasonable explanation that the console log actually represents a different state in time (when the code is 'halted').

还值得一读:仅使用 Array.push 时数组中的单元化变量?我在我的代码中也使用了 pop() ,这是一个合理的解释,即控制台日志实际上代表了不同的时间状态(当代码“停止”时)。