【问题标题】:I do not understand how var foo = [true].concat(Array.prototype.slice.call(arguments)) works, specifically what is [true] is doing?我不明白 var foo = [true].concat(Array.prototype.slice.call(arguments)) 是如何工作的,特别是 [true] 在做什么?
【发布时间】:2017-04-23 19:58:21
【问题描述】:

我正在查看 var args = [true].concat(Array.prototype.slice.call(arguments));我的理解是它试图将函数的参数添加到数组的末尾。但是我不熟悉语法 [true],在数组中具有 true。它试图达到什么目的。如果存在数组,则添加?如果不存在则做一个数组然后添加?

  extend: function extend(/* dest, source1, source2, ...*/) {
        var args = [true].concat(Array.prototype.slice.call(arguments));
        return copyInto.apply(this, args);
    },

【问题讨论】:

  • 这会将true 添加到数组的开头。
  • 刚开始的数组有一个值为true的元素
  • 它只是一个数组,其中一个元素的值为true。我猜copyInto 函数需要一个布尔值作为它的第一个参数。

标签: javascript


【解决方案1】:

[true] 创建一个由一个为 true 的布尔元素组成的数组。

例如这也是有效的:

[true, false, true]
[0,1,2]

[true].concat(some other array) 返回一个数组,它是 [true] 和其他一些数组的串联

例如

[true].concat([true, false, false])

等于[真、真、假、假]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 2011-10-26
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    相关资源
    最近更新 更多