एडम सही है; दोनों फ़ंक्शन अपरिभाषित लौटते हैं, और यदि आप वापसी मूल्य (या मूल्य को अपरिभाषित करने की इच्छा रखते हैं) की परवाह नहीं करते हैं तो किसी भी तरह से बिल्कुल ठीक है। हालांकि, यह अक्सर अधिक जटिल कार्यक्रमों में बेहतर होता है ताकि स्पष्ट रूप से किसी फ़ंक्शन से वापस आ सकें, खासकर जब जावास्क्रिप्ट प्रोग्राम में जटिल कॉलबैक तंत्र होते हैं।
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 के संबंधित भाग के लिए एक लिंक शामिल करना चाहिए: उदाहरण के लिए, कोड के इस टुकड़े (बस थोड़ी और तुम्हारा की तुलना में जटिल) मेरा मानना है कि में वापसी कथन वास्तव में कोड को स्पष्ट में मदद करता है कल्पना। – georg
वापसी; मूल रूप से यहां कार्य समाप्त करना है। तो यदि आपके पास यह आपके फ़ंक्शन के आखिरी बिट के रूप में है तो यह कुछ भी उपयोगी नहीं करता है। रुचि रखने वालों के लिए – gintas
12.9 spec में। – davin