简体   繁体   English

为什么在声明的对象上解析ES6有问题?

[英]Why there is a problem with destructuring ES6 on declared object?

I have a problem, or more some weird situation. 我有一个问题,或者更奇怪的情况。 I am using https://es6console.com . 我正在使用https://es6console.com

I want to use destructuring and assign properties to already declared variables. 我想使用解构并为已声明的变量赋值属性。 It looks like that there is a problem in where I declare object. 看起来在我声明对象的地方存在问题。 Please open https://es6console.com/jm6e72c7/ and click Transform to ES5. 请打开https://es6console.com/jm6e72c7/ ,然后单击“转换为ES5”。 There is a weird behaviour where I declare object after variables. 有一种奇怪的行为,我在变量之后声明了对象。

// not working
let ip, port;

let config = {
    ip: 'ip',
    port: 'port'
}

({ip, port} = config)

console.log(ip);

//working
let obj = {
    name: 'name',
    age: 'age'
}

let name, age;

({name, age} = obj)

console.log(name);

This is one of those situations where the semi-colon is mandatory: 这是必须使用分号的情况之一:

 let ip, port; let config = { ip: 'ip', port: 'port' }; //< --- you need the ; ({ip, port} = config) console.log(ip); 

Otherwise javascript will interpret the code as: 否则javascript会将代码解释为:

let config = {ip: 'ip',port: 'port'}() which is a type error because it tries to call a function.

First off, they both work. 首先,他们都工作。 You have two different ways to compile ES6 code to ES5: 您有两种不同的方法可以将ES6代码编译为ES5:

({ip, port} = config)
// converted to
((_config = config, ip = _config.ip, port = _config.port, _config));

// and

({name, age} = obj)
// converted to
name = obj.name;
age = obj.age;

The result, in both cases, is that the variables are set to the appropriate values from the object. 在这两种情况下,结果是变量被设置为来自对象的适当值。

The difference is that the transpiler thinks the return value of the assignment operation might be important in the first case, but won't be in the second case. 不同之处在于,转换器认为赋值操作的返回值在第一种情况下可能很重要,但不会在第二种情况下。 So in the first case, you'll see _config at the end as the return value. 所以在第一种情况下,你会在最后看到_config作为返回值。 It isn't needed, actually, but the transpiler is defensive -- it will do its best to make sure the functionality is exactly the same. 实际上并不需要它,但是转换器是防御性的 - 它将尽力确保功能完全相同

As to why it thinks that you might want the return value in the first case, it's because of the missing semi-colon after the declaration of the config object. 至于为什么它认为你可能想要第一种情况下的返回值,这是因为在声明config对象之后缺少分号。

With the semi-colon added, it works as expected: 添加了分号后,它按预期工作:

let config = {
    ip: 'ip',
    port: 'port'
};

({ip, port} = config)

Working example 工作实例

the problem is a missing semicolon. 问题是缺少分号。

 let ip, port; let config = { ip: 'ip', port: 'port' } ({ip, port} = config)//this is being evaluated as part of the let config declaration... console.log(ip); console.log('------------------------------'); let obj = { name: 'name', age: 'age' } let name, age; ({name, age} = obj) console.log(name); 

needs to be 需要是

 let ip, port; let config = { ip: 'ip', port: 'port' };//added semicolon here ({ip, port} = config);//not needed here, but good to have console.log(ip); console.log('------------------------------'); let obj = { name: 'name', age: 'age' } let name, age; ({name, age} = obj) console.log(name); 

you'll notice that, even when run as es6, you get a destructuring error on the first snippet. 你会注意到,即使以es6运行,你在第一个片段上也会出现解构错误。 this is because the interpreter is reading the statement as 这是因为解释器正在阅读声明

let ip, port;
let config = {ip:'ip',port:'port'}({ip, port} = config)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM