我怎么用js做到这一点?

时间:2020-03-04 11:17:10

标签: javascript function

你好,这是我的问题。我应该将此代码setTimeout返回[1,4,6,3,4,6].repeat()作为输出。我不明白如何设置分辨率。你能帮我吗?

3 个答案:

答案 0 :(得分:3)

在同一数组上使用.concat

var myArray = [1,4,6,3,4,6];
var newArray = myArray.concat(myArray);
console.log(newArray)

答案 1 :(得分:3)

这应该做到:

Array.prototype.repeat = function() {
  return [...this, ...this];
}

console.log([1,4,6,3,4,6].repeat())

请注意,更改内置类型的原型通常不是一个好主意,并且可能导致意外行为。

答案 2 :(得分:0)

这可能会帮助您解决问题。 它使您可以定义自己的数组函数。

Array.prototype.repeat= function(){
  return this.concat(this);
}

[1,4,6,3,4,6].repeat();