JavaScript 对象解构和别名 [英] JavaScript object destructuring and aliasing

查看:19
本文介绍了JavaScript 对象解构和别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 JavaScript 中解构对象并将本地解构对象别名?

Is there a way to destructure an object in JavaScript and alias the local destructured object?

类似于:

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

...并使 env 成为包含那些选定环境变量的局部常量.(我知道我的示例不适用于 babel)

...and have env become a local constant containing those selected environment variables. (I'm aware that my example doesn't work with babel)

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

有没有办法实现这种混叠?

Is there a way to achieve this aliasing?

顺便说一句,我使用 babel 作为我的转译器,然后使用 node 运行脚本,以便我可以利用更多的 ECMAScript 6 功能.

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.

推荐答案

根据我对规范的阅读,这应该是可解析的,但这并不意味着您的想法.它将从右到左解析为

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);

产地

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

与所有其他分配一样,被定义为向 RHS 求值,即 process.env.

is defined, like all other assignments, as evaluating to the RHS, which is process.env.

因此,您的代码等效于

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

验证这一点很容易:

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

导致一个新的常量 a 被声明为值 1,正如你所期望的.但是,bar 的值不是解构对象的别名",即{a: 1};它与 foo 相同,即 {a: 1, b: 2}.bar === foo.

results in a new constant a being declared with the value 1, as you would expect. However, the value of bar is not an "alias to the deconstructed object", which would be {a: 1}; it's the same as foo, which is {a: 1, b: 2}. bar === foo.

您正在尝试做的事情已经在 SO 和 ES 讨论组中多次讨论过.最重要的是,没有办法做到这一点.你能做的就是:

What you are trying to do has been discussed many times here on SO and also in the ES discuss group. 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, ...

我知道这很可怕.

您可以使用下划线的 pick 函数或编写自己的函数,但这需要您说

You could use the pick function from underscore or write your own, but that requires you to say

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

这只是稍微不那么可怕.

which is only slightly less horrible.

我们有休息属性"和扩展属性"建议用于 ES7,但它们似乎对我们没有帮助.

We have "rest properties" and "spread properties" proposed for ES7, but they do not seem to help us here.

我们需要的是一种新的语法,用于将属性挑选到另一个对象中.已经为此提出了各种建议,但没有一个得到了很大的关注.一种是扩展点符号",它允许将花括号结构放在点之后.在你的情况下,你会写

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};
                       ^^

您可以在此处了解有关此提案的更多信息.

这篇关于JavaScript 对象解构和别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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