简体   繁体   English

JavaScript object 解构和别名

[英]JavaScript object destructuring and aliasing

Is there a way to destructure an object in JavaScript and alias the local destructured object?有没有办法在 JavaScript 中解构 object 并别名为本地解构的 object?

Something like:就像是:

const env = {ENV_VAR_X, ENV_VAR_Y, ENV_VAR_Z} = process.env;

...and have env become a local constant containing those selected environment variables. ...并使env成为包含这些选定环境变量的局部常量。 (I'm aware that my example doesn't work with babel) (我知道我的例子不适用于 babel)

{
  ENV_VAR_X: "s867c7dsj4lal7", 
  ENV_VAR_Y: "hd73m20s-a=snf77f", 
  ENV_VAR_Z: "production"
}

Is there a way to achieve this aliasing?有没有办法实现这种别名?

On a side note, I'm using babel as my transpiler, then running the script with node so that I can leverage more ECMAScript 6 functionality.附带说明一下,我使用 babel 作为我的转译器,然后使用节点运行脚本,以便我可以利用更多 ECMAScript 6 功能。

According to my reading of the spec, this should be parseable, but it doesn't mean what you think.根据我对规范的阅读,这应该是可解析的,但这并不意味着您的想法。 It would be parsed from right-to-left as它将从右到左解析为

const env = ({ENV_VAR_X, ENV_VAR_Y, ENV_VAR_Z} = process.env);

where the production哪里生产

({ENV_VAR_X, ENV_VAR_Y, ENV_VAR_Z} = process.env)

is defined, like all other assignments, as evaluating to the RHS , which is process.env .与所有其他赋值一样,被定义为对 RHS 求值,即process.env

Therefore, your code is equivalent to因此,您的代码等效于

const {ENV_VAR_X, ENV_VAR_Y, ENV_VAR_Z} = process.env;
const env = process.env;

It's quite easy to validate this:验证这一点很容易:

const foo = {a: 1, b: 2};
const bar = {a} = foo;

results in a new constant a being declared with the value 1 , as you would expect.如您所料,结果声明了一个值为1的新常量a However, the value of bar is not an "alias to the deconstructed object", which would be {a: 1} ;但是, bar的值不是“解构对象的别名”,即{a: 1} it's the same as foo , which is {a: 1, b: 2} .它与foo相同,即{a: 1, b: 2} bar === foo . bar === foo


What you are trying to do has been discussed many times here on SO and also in the ES discuss group.您正在尝试做的事情已经在 SO 和 ES 讨论组中多次讨论过。 The bottom line is that there is no way to do this.底线是没有办法做到这一点。 All you can do is:你所能做的就是:

const {ENV_VAR_X, ENV_VAR_Y, ENV_VAR_Z} = process.env;
const env = {ENV_VAR_X, ENV_VAR_Y, ENV_VAR_Z};

In other words, there is no way to "deconstruct from an object into an object", or "pick from an object into an object".换句话说,没有办法“从一个对象解构到一个对象”,或者“从一个对象中选择一个对象”。 You can only deconstruct from an object into variables.您只能将对象解构为变量。

You could fall back to the old-fashioned way:你可以回到老式的方式:

const env = {ENV_VAR_X: process.env.ENV_VAR_X, ...

which I know is horrible.我知道这很可怕。

You could use the pick function from underscore or write your own, but that requires you to say您可以使用下划线的pick功能或编写自己的功能,但这需要您说

const env = _.pick(process.env, 'ENV_VAR_X', "ENV_VAR_Y', 'ENV_VAR_Z');

which is only slightly less horrible.这只是稍微不那么可怕。

We have "rest properties" and "spread properties" proposed for ES7, but they do not seem to help us here.我们为 ES7 提出了“rest 属性”和“spread 属性” ,但它们似乎对我们没有帮助。


What we need is a new syntax for picking properties into another object.我们需要的是一种新的语法,用于将属性挑选到另一个对象中。 There have been various proposals made for this, none of which has gained much traction.为此提出了各种建议,但没有一个得到了很大的关注。 One is the "extended dot notation", which allows a curly-bracketed construct to be put after a dot.一种是“扩展点符号”,它允许在点之后放置一个花括号结构。 In your case, you would write在你的情况下,你会写

const env = process.env.{ENV_VAR_X, ENV_VAR_Y, ENV_VAR_Z};
                       ^^

You can find out more about this proposal here .您可以 在此处了解有关此提案的更多信息。

To find assigning a new name to a destructed value, just use this:要找到为破坏的值分配新名称,只需使用:

obj = {x: 10, y: "Something"};
const { x: otherName } = obj;

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

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