ES6/ECMAScript6解构是否创建新变量? [英] Does ES6/ECMAScript6 destructuring create new variable?

查看:103
本文介绍了ES6/ECMAScript6解构是否创建新变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所知,我们使用解构在ES6中使用对象中的值.我想知道它的深度.

As you know, we use destructuring to use the values in objects with ES6. I wonder about its depth.

当我编写以下代码时:

let (or const) { firstVar, secondVar } = this.props

它是在内存中分配新空间还是使用指针等这些值?

does it allocate new space in memory or does it use these values like pointers?

过去,当我写信

let { options } = this.props
options.splice(0,20)

我注意到props中的变量已更改,尽管我的编译器在不同情况下会出现诸如"props are Readonly"之类的错误.怎么可能?

I noticed that the variable in props had changed although my compiler had errors like 'props are Readonly' on different cases. How could it be possible?

推荐答案

letconst声明新变量,是的.它们在分配/初始化之后独立于对象属性.

let and const declare new variables, yes. They are independent from the object property after the assignment/initialisation.

当我写let { options } = this.props; options.splice(0,20)时,我发现道具中的变量已经改变

when I wrote let { options } = this.props; options.splice(0,20) I noticed that the variable in props had changed

是的,这是因为您的选项是一个数组,并且变量和object属性都指向同一个可变数组实例. 分配未复制其内容.

Yes, that's because your options are an array, and both the variable and the object property point to the very same mutable array instance. Assignment did not copy its contents.

这篇关于ES6/ECMAScript6解构是否创建新变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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