javascript es6数组function“传播运算符”
我在示例代码之一中想到了这一点,我完全迷失了。
const addCounter = (list) => { return [...list, 0]; // This is the bit i am lost i now about [...list, 0] }
显然上面是等于下面的。
const addCounter = (list) => { return list.concat([0]); }
任何build议或解释非常感谢。
...list正在使用传播运算符来传播list的元素。 我们假设列表是[1, 2, 3] 。 因此[...list, 0]变成:
[1, 2, 3, 0]
与list.concat([0]);有相同的结果list.concat([0]);
这不是ES6中数组的特性,它只是用于数组连接。 它有其他用途。 阅读更多关于MDN ,或看到这个问题 。
...list spread (列出)数组list中的所有元素。
所以[...list, 0]是所有列表中的元素,最后是0