javascript - 使用关联数组作为变量名? - javascript

标签 javascript firefox firefox-addon associative-array

我正在查看来自 firefox 扩展的一些代码(此处:https://github.com/mozilla/prospector/blob/master/oneLiner/bootstrap.js#L34),我看到了一些我以前从未在 javascript 中看到的东西。程序员使用关联数组作为变量名。有人可以向我解释一下这个变量引用是如何工作的吗?

const {classes: Cc, interfaces: Ci, utils: Cu} = Components;

我通过阅读此页面了解“const”:https://developer.mozilla.org/en/JavaScript/Reference/Statements/const

但是它是如何使用关联数组对象作为变量名的呢?

此外,它似乎在关联数组中使用键名称作为对组件方法的引用(此处列出:https://developer.mozilla.org/en/Components_object)。我一直认为必须先有一个键名,然后才是值,但这似乎首先将引用的值放在组件类方法中,然后将其分配给 Cc 的名称,即使 Cc 位于值所在的位置会去(Ci 用于 Components interfaces 方法,Cu 用于 Components utils 方法)。

最佳答案

您看到的是解构赋值,自 javascript 1.7 起可用,请参阅此文档以获取更多信息 https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.7

Destructuring assignment makes it possible to extract data from arrays or objects using a syntax that mirrors the construction of array and object literals. The object and array literal expressions provide an easy way to create ad-hoc packages of data. Once you've created these packages of data, you can use them any way you want to. You can even return them from functions.

关于javascript - 使用关联数组作为变量名? - javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10350273/

相关文章:

javascript - 使用jspdf将pdf直接保存到文件

jquery - 在 Mozilla 中使用 tinymce 时获取 'NS_ERROR_UNEXPECTED'

javascript - Firefox 插件 Javascript 对象管理

javascript - 如何从 AJAX 向 Odoo 10 自定义模块 Controller 发出 POST/GET 请求? (被 CORS 政策阻止)

javascript - 导致移动设备水平滚动的 Facebook Like 按钮

selenium - Selenide 测试在 Firefox 中挂起

javascript - 查找来自 Firefox 扩展的所有 URL 请求

javascript - 如何识别 408 HTTP-Statuscode?

javascript - 需要 Angular ng-options 不能与 slim 模板一起使用

javascript - Chrome DevTools 命令行 API、jQuery、美元符号变量