同时不重新构造并分配给新的变量 [英] Destructure and assign to new variable at the same time not working

查看:119
本文介绍了同时不重新构造并分配给新的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  let {x} = a = {x:'cool'}; 
console.log(a,x);

其中输出:

 Firefox 
//引用错误:a没有定义 - 在Chrome,babel-node
中的对象{x:cool}酷 -

为什么有区别,什么是正确的行为?



更新:



这可能是更通用的用例。虽然这适用于所有环境:

  var a = b = {x:'cool'}; 

此语句在chrome / babel-node中不起作用:

  var {x} = a = {x:'cool'}; // SyntaxError:Unexpected token {

使用时也有不同的错误信息var 而不是 let

解决方案

在你的代码中,没有定义 a let a = b = c 不定义 b 。你需要说

  let a = {x:1},{x} = a; 

Firefox不支持 let 我想知道你的例子如何在那里工作。请参阅 https://kangax.github.io/compat-table/es6/


I am trying to destructure object and assign it to new variable at the same time:

let {x} = a = {x: 'cool'};
console.log(a, x);

which outputs:

//Object { x: "cool" } cool - in Firefox
//ReferenceError: a is not defined - in Chrome, babel-node

Why is there difference and what is the correct behavior?

UPDATE:

This is maybe more generic use case. While this works in all environments:

var a = b = {x: 'cool'};

this statement is not working in chrome/babel-node:

var {x} = a = {x: 'cool'}; //SyntaxError: Unexpected token {

There is also different error message when using var instead of let.

解决方案

In your code, a is not defined. let a = b = c does not define b. You need to say

let a = {x: 1}, {x} = a;

Firefox doesn't support let, so I'm wondering how your example worked there at all. See https://kangax.github.io/compat-table/es6/.

这篇关于同时不重新构造并分配给新的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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