将javascript函数翻译成coffee-script



谁能帮我把下面的翻译成coffeescript?

Step(
  function readSelf() {
    fs.readFile(__filename, this);
  },
  function capitalize(err, text) {
    if (err) throw err;
    return text.toUpperCase();
  },
  function showIt(err, newText) {
    if (err) throw err;
    console.log(newText);
  }
);

CoffeeScript等价如下。

Step (readSelf = ->
  fs.readFile __filename, @
), (capitalize = (err, text) ->
  throw err  if err?
  text.toUpperCase()
), showIt = (err, newText) ->
  throw err  if err?
  console.log newText

您可以使用此站点http://js2coffee.org/或您可以从https://github.com/rstacruz/js2coffee下载并安装代码并在您的机器上使用。

Step(
  readSelf = -> fs.readFile __filename, @
  capitalize = (err, text) ->
    throw err if err
    text.toUpperCase()
  showIt = (err, newText) ->
    throw err if err
    console.log newText
)

永远不要使用转换器。转换后,您的代码可能会损坏。例如代码,你可以看到在以前的帖子是不正确的。因为表达

throw err if err?

将生成:

if (typeof err !== "undefined" && err !== null) {
  throw err;
}

我认为这不是你期望看到的。我用咖啡创造者的网站来做我的咖啡实验。不要使用js2coffee站点,因为在转换过程中会出现一些严重的错误。我也有……好运!

最新更新