来自 Coffeescript 的臃肿 JS,它想要返回所有内容 [英] Bloated JS from Coffeescript which wants to return everything

查看:12
本文介绍了来自 Coffeescript 的臃肿 JS,它想要返回所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有这个 Coffeescript:

I've got this Coffeescript here:

brew = (args...) =>
  for e in args
    alert e
    null

brew('fo', 're', 'eo');

我希望我不需要把 null 放在那里让它工作,但是唉,编译成这样:

I wish I didn't need to put null there to get it to work, but alas, that compiles to this:

brew = function() {
  var args, e, _i, _len, _results;
  args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  _results = [];
  for (_i = 0, _len = args.length; _i < _len; _i++) {
    e = args[_i];
    alert(e);
    _results.push(null);
  }
  return _results;
};

brew('fo', 're', 'eo');

但现在我有 3 行不必要的:

But now I have 3 unnecessary lines:

 _results = [];
 _results.push(null);
 return _results;

有什么建议吗?

推荐答案

这个呢

brew = (args...) -> args.forEach alert

编译成

var brew,
    __slice = [].slice;

brew = function() {
  var args;
  args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  return args.forEach(alert);
};

brew('fo', 're', 'eo');

这篇关于来自 Coffeescript 的臃肿 JS,它想要返回所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆