Javascript 有返回语句和没有返回语句的函数之间有区别吗?
假设有两个相同的函数不返回值Javascript 有返回语句和没有返回语句的函数之间有区别吗?,javascript,Javascript,假设有两个相同的函数不返回值 function a() { // do some interesting things } function b() { // do the same interesting things return; } 函数b显然更详细,但它们之间有什么函数上的区别吗?没有真正的区别;两者都将返回未定义的 没有return语句的函数将返回未定义的,带有空return语句的函数也是如此 要自己确认这一点,您可以运行以下代码--: 通常,您返回的是
function a() {
// do some interesting things
}
function b() {
// do the same interesting things
return;
}
函数
b
显然更详细,但它们之间有什么函数上的区别吗?没有真正的区别;两者都将返回未定义的
没有return语句的函数将返回未定义的,带有空return
语句的函数也是如此
要自己确认这一点,您可以运行以下代码--:
通常,您返回的是一个值。那么比如说,
function b() {
return 'hello';
}
a = b();
console.log(a);
将向您的控制台输出“hello”。Adam是正确的;这两个函数都返回未定义的值,如果您不关心返回值(或希望该值未定义),则任何一种方法都是完全正确的。然而,在更复杂的程序中,从函数显式返回通常更好,特别是因为Javascript程序通常具有复杂的回调机制。例如,在这段代码中(比您的代码稍微复杂一点),我相信return语句确实有助于澄清代码:
function someAsyncFunction(someVar, callback) {
// do something, and then...
callback(someVar);
// will return undefined
return;
}
function c(){
var someState = null;
if (some condition) {
return someAsyncFunction(some variable, function () {
return "from the callback but not the function";
});
// we've passed the thread of execution to someAsyncFunction
// and explicitly returned from function c. If this line
// contained code, it would not be executed.
} else if (some other condition) {
someState = "some other condition satisfied";
} else {
someState = "no condition satisfied";
}
// Note that if you didn't return explicitly after calling
// someAsyncFunction you would end up calling doSomethingWith(null)
// here. There are obviously ways you could avoid this problem by
// structuring your code differently, but explicit returns really help
// avoid silly mistakes like this which can creep into complex programs.
doSomethingWith(someState);
return;
}
// Note that we don't care about the return value.
c();
一个好的答案应该包括一个指向ECMA规范返回相关部分的链接;这里基本上是指终止函数。因此,如果你把它作为你函数的最后一部分,它不会做任何有用的事情。规范中的12.9,对那些感兴趣的人来说。完全主观,但我完全不同意。我不认为这会使代码更容易阅读。相反,它是不必要的,所以它只会造成混乱。我的2c。我想每个人都有自己的。
function someAsyncFunction(someVar, callback) {
// do something, and then...
callback(someVar);
// will return undefined
return;
}
function c(){
var someState = null;
if (some condition) {
return someAsyncFunction(some variable, function () {
return "from the callback but not the function";
});
// we've passed the thread of execution to someAsyncFunction
// and explicitly returned from function c. If this line
// contained code, it would not be executed.
} else if (some other condition) {
someState = "some other condition satisfied";
} else {
someState = "no condition satisfied";
}
// Note that if you didn't return explicitly after calling
// someAsyncFunction you would end up calling doSomethingWith(null)
// here. There are obviously ways you could avoid this problem by
// structuring your code differently, but explicit returns really help
// avoid silly mistakes like this which can creep into complex programs.
doSomethingWith(someState);
return;
}
// Note that we don't care about the return value.
c();