当我们使用数组名称而不是扩展运算符时有什么区别? [英] What is the difference when we use array names instead of spread operator?

查看:88
本文介绍了当我们使用数组名称而不是扩展运算符时有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用有什么区别:

What is the difference if I use:

var numbers = [1, 2, 3]
var mainArray = (numbers.length > 1) ? numbers : '';

而不是:

var numbers = [1, 2, 3]
var mainArray = (numbers.length > 1) ? [...numbers] : '';


推荐答案

由于数据结构的分配指向同一空间内存,如果你有两个引用相同数组的变量,改变一个变量将改变另一个。也就是说:

Since assigment of data structures points to the same space in memory, if you have two variables referencing the same array, altering one variable will alter the other. That is to say:

如果 x = [1,2,3]
y = x
然后我们说 x.push(5) y 也会有5,因为它们指向同一个实例。如果您使用 [... x] ,则表示您正在创建x的副本。它消耗O(n)内存,一个新的参考。因此,如果x被改变,y将不受影响。

if x = [1, 2, 3] and y = x then we say x.push(5) y will also have that 5 because they are pointing to the same instance. If you use [...x] you are creating a copy of x. It consumes O(n) memory, a new reference. So if x is altered, y will be uneffected.

点差是干净整洁但它们确实有开销。如果在非常大的数据集上使用它们将影响性能。

spreads are clean and neat but they do have overhead. They will effect performance if used on a very large data set.

这篇关于当我们使用数组名称而不是扩展运算符时有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆