为什么在Javascript中销毁时使用{source:target}?

为什么在Javascript中销毁时使用{source:target}?,javascript,Javascript,为什么在Javascript中分解为新变量时,它是const{source:target}=obj,而不是相反 我这样问是因为const{target:soruce}=obj更符合 const source = 'x'; const obj = {target:source}; …我会犯更少的错误。:) 如果将其格式化为: ({ source: target } = { source: "value" }); 特别是对于嵌套属性: ({ source1: { source2: tar

为什么在Javascript中分解为新变量时,它是
const{source:target}=obj
,而不是相反

我这样问是因为
const{target:soruce}=obj
更符合

const source = 'x';
const obj = {target:source};

…我会犯更少的错误。:)

如果将其格式化为:

 ({ source: target  } =
  { source: "value" });
特别是对于嵌套属性:

({ source1: { source2: target  }} =
 { source1: { source2: "value" }});

如果将其格式设置为:

 ({ source: target  } =
  { source: "value" });
特别是对于嵌套属性:

({ source1: { source2: target  }} =
 { source1: { source2: "value" }});

你的问题很有趣。因此,我想谈谈我的看法:

常量{source:target}=object

1) 双方应该平等:

x = x
2) 因此,两者都是对象:

{ source } = object
3) 如果对象不知何故是未定义的呢

{ source } = object || { source: 'the value - not an alias' }
4) 提取的是键,而不是值

const { source } // and source will hold the value
5) 如果它被用作

const { source as target } = object
// like we have
// export { source as target } from 'module'
嵌套对象关键点将非常困难:

const obj = { source: { target: 'another value' } }
const { source: { target: asValue } } = obj
如果我们使用as,您能思考吗?

const { source of target as asValue } // makes life no easier.
但是使用导入/导出语法只有一个考虑因素

import { a, b, c } from 'module'
但不是:

import { a, b, c } from 'module', 'another-module', 'third-module'
希望,这是有意义的


如果您实际上一直在使用解构语法,那么您可能会看到并遵循my中提供的链接。

您的问题很有趣。因此,我想谈谈我的看法:

常量{source:target}=object

1) 双方应该平等:

x = x
2) 因此,两者都是对象:

{ source } = object
3) 如果对象不知何故是未定义的呢

{ source } = object || { source: 'the value - not an alias' }
4) 提取的是键,而不是值

const { source } // and source will hold the value
5) 如果它被用作

const { source as target } = object
// like we have
// export { source as target } from 'module'
嵌套对象关键点将非常困难:

const obj = { source: { target: 'another value' } }
const { source: { target: asValue } } = obj
如果我们使用as,您能思考吗?

const { source of target as asValue } // makes life no easier.
但是使用导入/导出语法只有一个考虑因素

import { a, b, c } from 'module'
但不是:

import { a, b, c } from 'module', 'another-module', 'third-module'
希望,这是有意义的


如果您实际上一直在使用解构语法,那么您可能会看到并遵循my中提供的链接。

target
是以这种方式解构时
属性的别名。如果它是
const{source as target}=obj
like
import{source as target}from'lib'
I将不会有任何问题。将不得不询问建立语法的委员会。虽然考虑到
:target
位是可选的,但是
const{source:target}=obj
更有意义,但是它被清楚地记录在案。只有行
const{source}=obj
创建一个
source
变量,该变量等于
obj[“source”]
。const{source:target}为您提供了一个创建变量名的选项,该变量名与您要定位的键不同。使用
as
或执行反向操作在处理嵌套的分解时也会变得丑陋
target
是分解时
source
属性的别名。如果它是
const{source as target}=obj
like
import{source as target}from'lib'
I将不会有任何问题。将不得不询问建立语法的委员会。虽然考虑到
:target
位是可选的,但是
const{source:target}=obj
更有意义,但是它被清楚地记录在案。只有行
const{source}=obj
创建一个
source
变量,该变量等于
obj[“source”]
。const{source:target}为您提供了一个创建变量名的选项,该变量名与您要定位的键不同。在处理嵌套的解构时,将
用作
或执行反向操作也会变得很糟糕