[英]Javascript const with curly braces which have 2 variable separated by colon
I'm working on a react native project, I found a solution on internet to my problem but I don't understand one line from a function 我正在做一个本机反应项目,我在网上找到了解决我问题的方法,但是我不理解函数中的任何一行
componentDidUpdate(propsOld) { // line 1
const { fill } = this.props; // line 2
const { fill:fillOld } = propsOld; // line 3
if (fill !== fillOld) { // line 4
Animated.timing(this.anim, { toValue:fill, duration:2000 // line 5 }).start();
}
}
The line that I don't understand is the line 3: 我不明白的那行是第3行:
const { fill:fillOld } = propsOld;
I understand the use of curly braces when there is a single variable or multiple variables seperated by comma ',', 我理解当单个变量或多个变量用逗号','分开时使用花括号的情况,
Would anyone please explain to me the meaning when they are separated by colon ':' ? 有人用冒号':'分隔时,请给我解释一下的意思吗?
using a : instead of a , is that it created an alias for fill. 使用:而不是,是它创建了填充别名。 so fill can now be referenced as fillOld.
因此现在可以将fill称为fillOld。 so If i wanted bill to be known as fred I would do { bill:fred }
因此,如果我想让条例草案被称为弗雷德,我会做{bill:fred}
basically that is renaming a variable when destructuring the object. 基本上,这是在销毁对象时重命名变量。
if you have an object like this: 如果您有这样的对象:
const obj = {
prop1: 'prop1',
prop2: 'prop2',
prop3: 'prop3',
}
and you want to get your variable prop2
, you can do it like this: 而您想获取变量
prop2
,则可以这样进行:
const { prop2 } = obj;
but what if you already defined something with the same name? 但是,如果您已经定义了相同的名称怎么办? a block like this where you have
prop4
defined and you want to get that variable when destructuring? 像这样的块,其中您已经定义了
prop4
并且想要在prop4
时获取该变量?
const prop4 = 'something';
const obj = {
prop1: 'prop1',
prop2: 'prop2',
prop3: 'prop3',
prop4: 'else'
}
you can not do: const { prop4 } = obj;
您不能这样做:
const { prop4 } = obj;
because prop4
already exist and also it is a const
因为
prop4
已经存在,而且它是一个const
so basically you can rename it like this: 所以基本上您可以像这样重命名它:
const { prop4: prop4duplicated } = obj;
so basically, on your code: 所以基本上,在您的代码上:
const { fill } = this.props;
const { fill:fillOld } = propsOld;
it is generating 2 variables, fill
and fillOld
, that is because fill
already exists, then it is being renamed to fillOld
它正在生成2个变量,
fill
和fillOld
,这是因为fill
已经存在,然后将其重命名为fillOld
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.