使用coffeescript中的参数显示模块模式

时间:2012-03-01 16:07:53

标签: javascript coffeescript

我很好奇是否有人对以下两种方式有任何偏好来揭示Revealing模块模式中的参数:

图表1:

DocReview.DocumentsApp = (
    ((DocReview, Backbone) ->
        console.log("body")
    )(window.DocReview, window.Backbone)) 

编译为

 DocReview.DocumentsApp = (function(DocReview, Backbone) {
    return console.log("body");
  })(window.DocReview, window.Backbone);

图表2:

DocReview.DocumentsApp = do(DocReview = window.DocReview, Backbone = window.Backbone) ->
    console.log("body")

编译为

  DocReview.DocumentsApp = (function(DocReview, Backbone) {
    if (DocReview == null) DocReview = window.DocReview;
    if (Backbone == null) Backbone = window.Backbone;
    return console.log("body");
  })(DocReview, Backbone);

我认为我将第二种方式与do关键字进行比较,因为它是更整洁的coffeescript,但我不确定。

有人就此事发表意见吗?

1 个答案:

答案 0 :(得分:1)

另一种选择:

DocReview.DocumentsApp = do ({DocReview, Backbone} = window) ->
    console.log "body"

编译为:

DocReview.DocumentsApp = (function(_arg) {
  var Backbone, DocReview, _ref;
  _ref = _arg != null ? _arg : window, DocReview = _ref.DocReview, Backbone = _ref.Backbone;
  return console.log("body");
})({
  DocReview: DocReview,
  Backbone: Backbone
});