【问题标题】:Best way to iterate over an array and call functions in coffeescript在咖啡脚本中迭代数组和调用函数的最佳方法
【发布时间】:2012-04-25 06:59:39
【问题描述】:

我在 coffescript 中有这段代码

copy pages.template  for pages in configFiles.pages

在java脚本中生成这段代码

var pages, _i, _len, _ref;

_ref = configFiles.pages(function() {});
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  pages = _ref[_i];
  copy(pages.template);
}

但我想要的是在 for 中再调用 2 个函数,如下所示:

var pages, _i, _len, _ref;

_ref = configFiles.pages(function() {});
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  pages = _ref[_i];
  copy(pages.template);
  copy2(pages.template);
  copy3(pages.template);
}

我不知道这是否是一个好习惯。我是编程界的新手。 如果是我怎么能在coffeescript中做到这一点?如果不是最好的解决方案是什么?

谢谢

【问题讨论】:

    标签: javascript arrays coffeescript loops


    【解决方案1】:

    您可以将块语法用于循环,而不是将其嵌套在理解中:

    for pages in configFiles.pages
        copy(pages.template)
        copy1(pages.template)
        copy2(pages.template)
    

    【讨论】:

      【解决方案2】:

      不推荐,但技术上可以将它们塞进一行:

      (copy pages.template; copy1 pages.template; copy2 pages.template) for pages in configFiles.pages
      

      【讨论】:

        【解决方案3】:

        在coffeescript 中,您可以嵌套列表推导/for 循环。所以你可以做这样的事情。

        (copy pages.template for pages in configFiles.pages for num in [3..1])
        

        【讨论】:

          猜你喜欢
          • 2012-01-17
          • 1970-01-01
          • 2012-12-31
          • 1970-01-01
          • 2015-02-13
          • 2012-05-14
          • 1970-01-01
          • 1970-01-01
          • 2012-07-16
          相关资源
          最近更新 更多