CoffeeScript中的对象文字多重赋值

时间:2015-06-10 02:00:49

标签: javascript json node.js coffeescript

我在Javascript中有点新手。我正在查看Atom软件包的一些Coffeescript代码,我偶然发现了这段代码:

loadProperties: ->
    @properties = {}
    fs.readFile path.resolve(__dirname, '..', 'completions.json'), (error, content) =>
      {@pseudoSelectors, @properties, @tags} = JSON.parse(content) unless error?
      return

我对最后一行{@pseudoSelectors, @properties, @tags} = JSON.parse(content) unless error?感到有点困惑,因为它似乎从解析的JSON内容中分配了多个值。在我的困惑中,我决定使用js2Coffee将其转换回Javascript,最后我得到了以下内容:

function() {
this.properties = {}; // make list of properties (global to provider)
return fs.readFile(path.resolve(__dirname, '..', 'completions.json'), (function(_this) { //load completions.json (using path module)
  return function(error, content) { // edit: nvm, js2coffee's fault. not sure why they wrapped the call back in another anonymous function, but this is a node stream callback
    var ref;
    if (error == null) { // if there are no errors
      ref = JSON.parse(content), _this.pseudoSelectors = ref.pseudoSelectors, _this.properties = ref.properties, _this.tags = ref.tags;
    }
  };
})(this));

这段代码比上面的代码更容易理解。我可以看到ref被分配了从内容流解析的对象,然后用于为其他变量分配其指定的数据。我的问题是,这种类型的任务如何运作?在Coffeescript中,预处理器如何知道分配值的位置以及分配它们的顺序?

通过检查completions.json,数据不符合分配的顺序。

1 个答案:

答案 0 :(得分:2)

这称为Destructuring Assignment

  

为了更方便地从复杂数组和对象中提取值,CoffeeScript实现了ECMAScript Harmony提出的destructuring assignment语法。将数组或对象文字指定给值时,CoffeeScript会分解并将两边相互匹配,将右侧的值分配给左侧的变量。

CoffeeScript将=左侧的对象或数组解释为模式,与使用的名称相匹配......

  • @pseudoSelectors
  • @properties
  • @tags

...到指定值内的属性或索引:

  • JSON.parse(content).pseudoSelectors
  • JSON.parse(content).properties
  • JSON.parse(content).tags

(定义额外的ref以避免为每个JSON.parse(content)重新评估。{/ p>

至于订单,CoffeeScript通常会使用他们在作业中提到的顺序。将@pseudoSelectors移动到模式中的第3个属性将在生成的JavaScript中回显。

{@properties, @tags, @pseudoSelectors} = JSON.parse(content) unless error?
var ref;

if (typeof error === "undefined" || error === null) {
  ref = JSON.parse(content),
    this.properties = ref.properties,
    this.tags = ref.tags,
    this.pseudoSelectors = ref.pseudoSelectors; // now last
}

虽然JavaScript ObjectsJSON.parse(content)的结果一样,但并未强制作为排序数据结构。如果您需要确保值的顺序,则必须使用Array

相关问题