[英]translating javascript function to 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网站,转换中有一些错误可能很关键。 我有一个..祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.