Atom 包 - [workspaceElement, activationPromise] = []

Atom Package - [workspaceElement, activationPromise] = []

在 Atom 包的规范(测试)部分,似乎有这样初始化变量的约定:

[workspaceElement, activationPromise] = []
[workspaceElement, initialActiveItem, otherItem1, otherItem2] = []

这似乎被翻译成 Vanilla JS 为:

var initialActiveItem, otherItem1, otherItem2, ref, workspaceElement;

ref = [], workspaceElement = ref[0], initialActiveItem = ref[1], otherItem1 = ref[2], otherItem2 = ref[3];

但随后这些变量会像这样单独初始化:

initialActiveItem = atom.workspace.getActiveTextEditor()

它们也是分开使用的。那么,赋值语句有什么意义呢?

好的,我自己想出来了。

在 Jasmine 中,这些变量在 setup/teardowns 中使用,例如 beforeEach 函数。它们也用于实际测试用例。因此,它们必须附加到套件的词法范围。

这不能用 CoffeeScript 自然地完成,因为无法直接访问 var,除非使用反引号 Vanilla 块。

所以[var1, var2...] = []是在当前范围内声明var的习惯用法。