简体   繁体   English

array.concat() 方法是否正确

[英]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:它通过了我所有的测试:

  1. return new array only passing array argument.仅通过数组参数返回新数组。
  2. passing two parameters second one is string.传递两个参数第二个是字符串。
  3. passing two parameters second one is array.传递两个参数第二个是数组。
  4. passing two parameters second one is number.传递两个参数第二个是数字。
  5. passing three parameters second is number, third is string.传递三个参数第二个是数字,第三个是字符串。
  6. passing four arguments.传递四个参数。
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM