Nodejs coffee为什么每次都会生成 return语句
Nodejs coffee为什么每次都会生成 return语句
alert(
try
nonexistent / undefined
catch error
“And the error is … #{error}”
)
如果我不想在 catch 语句中出现 return 怎么处理?
为什么它每次都会在代码块的最后一句生成 return ?
var error;
alert((function() {
try {
return nonexistent / void 0;
} catch (_error) {
error = _error;
return "And the error is … " + error;
}
})());
在 Node.js 中使用 CoffeeScript 编写代码时,你可能会遇到一个问题,即在 catch
语句中每次都会生成一个 return
语句。这通常是由于 CoffeeScript 的语法和编译规则导致的。
问题分析
CoffeeScript 在编译时会将 try...catch
结构转换为 JavaScript 的 try...catch
结构,并且在 catch
语句中生成 return
语句。这是因为 CoffeeScript 将整个表达式视为一个返回值的函数,因此需要确保每个分支都能返回一个值。
示例代码
假设我们有以下 CoffeeScript 代码:
alert(
try
nonexistent / undefined
catch error
"And the error is ... #{error}"
)
编译后的 JavaScript 代码如下:
(function() {
var error;
alert((function() {
try {
return nonexistent / void 0;
} catch (_error) {
error = _error;
return "And the error is ... " + error;
}
})());
})();
解决方案
如果你不想在 catch
语句中生成 return
语句,可以考虑使用其他方法来处理错误。例如,你可以将错误处理逻辑放在一个单独的函数中,而不是直接嵌入到 try...catch
结构中。
示例代码
handleError = (error) ->
console.log "And the error is ... #{error}"
try
nonexistent / undefined
catch error
handleError(error)
alert("An error occurred.")
编译后的 JavaScript 代码如下:
(function() {
var handleError;
handleError = function(error) {
return console.log("And the error is ... " + error);
};
try {
nonexistent / void 0;
} catch (error) {
handleError(error);
alert("An error occurred.");
}
})();
解释
在这个解决方案中,我们将错误处理逻辑封装在一个单独的函数 handleError
中。这样,在 catch
语句中只需要调用这个函数即可,而不需要返回一个值。这种方式更加清晰,也避免了不必要的 return
语句。
通过这种方式,你可以更好地控制错误处理流程,并使代码更具可读性和可维护性。
在结束的地方加一句 return,就不会生成return了
函数式风格的编程语言就是这样,它们的函数都会返回最后那个值,ruby 和 lisp 也这样。
可以看一下这个issue的讨论:https://github.com/jashkenas/coffeescript/issues/2477
jashkenas大神明确表示不会改变这个行为,CS的理念是expression-oriented
的,即任何代码片段都应该是一个有意义的expression,绝大部分情况下默认的return都不会造成任何问题,不用管就是了。甚至有人已经提了很有意思的PR,也被拒绝了 https://github.com/jashkenas/coffeescript/pull/2726 ,jashkenas的拒绝理由:
I’m afraid I’m going to have to close this for the same reasons as before – It’s a great PR, and a great example implementation, but the reasoning for the current function options haven’t changed. Ideally every function returns a meaningful value – if it’s asynchronous, maybe a promise – even if that value is just true or null, or this for chaining. And if you really don’t want to return a value, the nicest place to put that information is at the end of the function, where the value normally comes out. Anyhow, it’s all been said before ;)
这种理念对强迫症患者来说会觉得很别扭,可实际上一旦接受了这种设定,还觉得挺带感的……
有个回复我非常认同,CS不是JS的一个Wrapper,而是JS Runtime上的另一门不同的,并且更好的语言。所以不要去纠结生成的JS代码了,按CS本身提供的语法、语义和编程理念来写代码,能写出更优雅的代码,而且能得到更好的编程思维
对于你这个问题,这些return只要不造成影响,不要管就是了
在CoffeeScript中,每个表达式或代码块默认会返回其最后一行的值。在你的例子中,catch
语句中的表达式会返回 "And the error is ... #{error}"
,因此整个函数也会返回这个值。
如果你想避免在 catch
语句中生成 return
,你可以通过调整代码结构来实现。以下是一些方法:
方法1:使用一个变量存储结果
try
nonexistent / undefined
catch error
errorMessage = "And the error is ... #{error}"
errorMessage ?= "No error occurred"
alert errorMessage
方法2:直接调用alert
函数
try
nonexistent / undefined
catch error
alert "And the error is ... #{error}"
示例代码解释
-
方法1:通过将错误信息赋值给一个变量
errorMessage
,你可以控制是否需要显示错误信息。如果捕获不到错误,则errorMessage
会被设置为默认值"No error occurred"
。 -
方法2:直接在
catch
语句中调用alert
函数,这样就不会生成return
语句。
这两种方法都可以避免在 catch
语句中生成 return
,从而使你的代码更加清晰和灵活。