[英]Is it correct or not array.concat() method
I created concat function similar to Array.concat().我创建了类似于 Array.concat() 的 concat 函数。 It is passing all my tests:
它通过了我所有的测试:
function concat() {
var result = [];
for (var i = 0; i < arguments.length; i++) {
result.push(arguments[i]);
}
return result;
}
This is more like Array.of
as mentioned in comments:这更像是
Array.of
中提到的Array.of
:
console.log(Array.of(1, 2, 3));
But you can make the exact same functionality much more concise with rest parameters and the implicit return of arrow functions:但是您可以使用 rest 参数和箭头函数的隐式返回使完全相同的功能更加简洁:
const concat = (...a) => a; console.log(concat()); console.log(concat(1, 2, 3));
Although, since it is Array.of
, it's so much easier just to assign the function reference:虽然,因为它是
Array.of
,所以分配函数引用要容易得多:
const concat = Array.of; console.log(concat()); console.log(concat(1, 2, 3));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.