带块的常量声明 [英] Constant declaration with block

查看:147
本文介绍了带块的常量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在研究Firefox Add-on Builder SDK源代码,以及偶然发现了这样的常量声明:

Recently I was looking into Firefox Add-on Builder SDK sources, and stumbled on such constants declaration:

const { getCodeForKey, toJSON } = require("../../keyboard/utils");

我可以找到有关 CommonJS模块,但是这项任务的左侧部分让我感到困惑,因为它必须是特定于语言的,而且我无法谷歌上的任何内容。

I could find information about CommonJS Modules, but left part of this assignment slightly confuses me, since it must be language specific, and I couldn't google anything on that.

有人可以指点我的一些规格/草案来解释这里发生了什么吗?

Can someone point me to some specification/draft that explains what's going on here?

推荐答案

这是一个解构分配,目前只有Firefox使用的SpiderMonkey JavaScript引擎。以下是它如何与数组一起使用:

This is a destructuring assignment, something that is currently only implemented by the SpiderMonkey JavaScript engine which is used by Firefox. Here is how it works with arrays:

// Destructuring assignment
[a, b] = foo;

// Equivalent code
a = foo[0];
b = foo[1];

以下是对象的工作原理:

And here is how it works with objects:

// Destructuring assignment
{a, b} = foo;

// Equivalent code
a = foo.a;
b = foo.b;

稍微复杂的例子:

// Destructuring assignment
{name: a, address: {line1: b}} = foo;

// Equivalent code
a = foo.name;
b = foo.address.line1;

所以你的代码示例相当于:

So your code example is equivalent to:

var utilsExports = require("../../keyboard/utils");
const getCodeForKey = utilsExports.getCodeForKey;
const toJSON = utilsExports.toJSON;

这只是一种更方便的写作方式。

It is merely a more convenient way to write it.

这篇关于带块的常量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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