Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用call方法的javascript concat_Javascript_Prototype - Fatal编程技术网

使用call方法的javascript concat

使用call方法的javascript concat,javascript,prototype,Javascript,Prototype,我试图使用concat()方法在javascript中连接两个数组。我想知道这两种情况有什么不同 var a = [1, 2, 3]; console.log(Array.prototype.concat.call(a, [4, 5, 6])); // Result: [1, 2, 3, 4, 5, 6] console.log(Array.prototype.concat(a, [4, 5, 6])); // Result: [1, 2, 3, 4, 5, 6] 我认为通过使用call方法

我试图使用
concat()
方法在javascript中连接两个数组。我想知道这两种情况有什么不同

var a = [1, 2, 3];
console.log(Array.prototype.concat.call(a, [4, 5, 6]));
// Result: [1, 2, 3, 4, 5, 6]

console.log(Array.prototype.concat(a, [4, 5, 6]));
// Result: [1, 2, 3, 4, 5, 6]

我认为通过使用
call
方法,当我们将
这个
对象作为第一个参数传入时,它会改变数组
a
。但是它没有。Array.prototype.concat返回一个新数组。所以
Array.prototype.concat(a[4,5,6])
Array.prototype.concat.call(a[4,5,6])
实际上做了同样的事情。如果要更改原始数组,应该使用
array.prototype.push.apply(a[4,5,6])
array.prototype.concat
返回一个新数组。所以
Array.prototype.concat(a[4,5,6])
Array.prototype.concat.call(a[4,5,6])
实际上做了同样的事情。如果要更改原始数组,应该使用
array.prototype.push.apply(a[4,5,6])


Array.prototype.concat.call(a[4,5,6])

相当于
a.concat([4,5,6])
(假设
a.concat==Array.prototype.concat
,在您的示例中,
a数组的实例就是这种情况)


Array.prototype.concat(a[4,5,6])

相当于
Array.prototype.concat.call(Array.prototype,a[4,5,6])

假设
Array.prototype
是一个空数组,则两者产生相同的结果<代码>[].concat(a[4,5,6])
Array.prototype.concat.call([],a[4,5,6])
也可以


Array.prototype.concat.call(a[4,5,6])

相当于
a.concat([4,5,6])
(假设
a.concat==Array.prototype.concat
,在您的示例中,
a数组的实例就是这种情况)


Array.prototype.concat(a[4,5,6])

相当于
Array.prototype.concat.call(Array.prototype,a[4,5,6])


假设
Array.prototype
是一个空数组,则两者产生相同的结果
[].concat(a[4,5,6])
Array.prototype.concat.call([],a[4,5,6])
也可以。

concat
不会变异任何内容,它返回一个新数组。不知道你想做什么。@Bergi谢谢你的回复。.在mozilla文档中,他们在concat上使用了call()。我试图理解call()的用例是什么..当你不用call()就能实现同样的事情时,你指的是哪个Mozilla文档?请链接它<当
a
不是数组且本身没有
concat
方法时,通常需要使用code>call
。@wildwide如果您不确定数组实例本身是否覆盖了该方法,则可以使用prototype.call方法。如果有人用另一个实例重新分配实例本身的.concat属性,则使用prototype方法可以保护您implementation@WildWidow: . 在这里使用
call
没有任何意义(并且调用
[]
数组的别名。prototype是完全错误的)。希望有一些更有意义的例子。
concat
没有任何变化,它返回一个新数组。不知道你想做什么。@Bergi谢谢你的回复。.在mozilla文档中,他们在concat上使用了call()。我试图理解call()的用例是什么..当你不用call()就能实现同样的事情时,你指的是哪个Mozilla文档?请链接它<当
a
不是数组且本身没有
concat
方法时,通常需要使用code>call。@wildwide如果您不确定数组实例本身是否覆盖了该方法,则可以使用prototype.call方法。如果有人用另一个实例重新分配实例本身的.concat属性,则使用prototype方法可以保护您implementation@WildWidow: . 在这里使用
call
没有任何意义(并且调用
[]
数组的别名。prototype是完全错误的)。希望有一些更有意义的例子。