var ar = [];
console.log(ar.push([])); // 1
我原以为日志是[[]]
,但它告诉我 1. 代码上面发生了什么。
最佳答案
根据 MDN ,Array#push
返回:
The new length property of the object upon which the method was called.
因此,在您的情况下,当您将一个元素插入一个空数组时 - 这使得长度为 1 - 因此是结果。
如果您想确认:
// Empty array.
var arr = [];
// Push an item: result = 1
console.log(arr.push([]));
// Push another item: result = 2
console.log(arr.push([]));
关于javascript - 它是如何工作的将空数组插入空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49080235/