2012-02-24 9 views
6

आप 2 समान कार्यों कि एक मूल्यक्या रिटर्न स्टेटमेंट के साथ और बिना किसी फ़ंक्शन के बीच कोई अंतर है?

function a() { 
    // do some interesting things 
} 

function b() { 
    // do the same interesting things 
    return; 
} 

समारोह b स्पष्ट रूप से अधिक वर्बोज़ है लेकिन इन दोनों के बीच किसी भी कार्यात्मक फर्क है नहीं लौटते है कहो?

+2

यह करने के लिए एक अच्छा जवाब ECMA के संबंधित भाग के लिए एक लिंक शामिल करना चाहिए: उदाहरण के लिए, कोड के इस टुकड़े (बस थोड़ी और तुम्हारा की तुलना में जटिल) मेरा मानना ​​है कि में वापसी कथन वास्तव में कोड को स्पष्ट में मदद करता है कल्पना। – georg

+2

वापसी; मूल रूप से यहां कार्य समाप्त करना है। तो यदि आपके पास यह आपके फ़ंक्शन के आखिरी बिट के रूप में है तो यह कुछ भी उपयोगी नहीं करता है। रुचि रखने वालों के लिए – gintas

+1

12.9 spec में। – davin

उत्तर

10

कोई वास्तविक अंतर नहीं है; दोनों undefined वापस आ जाएंगे।

कोई रिटर्न स्टेटमेंट के साथ फ़ंक्शन undefined लौटाएंगे, क्योंकि एक खाली return कथन के साथ कार्य करेगा।


खुद के लिए यह पुष्टि करने के लिए, तो आप इस कोड चला सकते हैं - FIDDLE:

​function a() { 
} 

function b() { 
    return; 
} 

var aResult = a(); 
var bResult = b(); 

alert(aResult === bResult); //alerts true 
1

आम तौर पर आप एक मूल्य के लिए लौट रहे हैं। तो उदाहरण के लिए,

function b() { 
    return 'hello'; 
} 

a = b(); 

console.log(a); 

आपके कंसोल पर "हैलो" आउटपुट करेगा।

3

एडम सही है; दोनों फ़ंक्शन अपरिभाषित लौटते हैं, और यदि आप वापसी मूल्य (या मूल्य को अपरिभाषित करने की इच्छा रखते हैं) की परवाह नहीं करते हैं तो किसी भी तरह से बिल्कुल ठीक है। हालांकि, यह अक्सर अधिक जटिल कार्यक्रमों में बेहतर होता है ताकि स्पष्ट रूप से किसी फ़ंक्शन से वापस आ सकें, खासकर जब जावास्क्रिप्ट प्रोग्राम में जटिल कॉलबैक तंत्र होते हैं।

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(); 
+3

पूरी तरह से व्यक्तिपरक, लेकिन मैं पूरी तरह से असहमत हूं। मुझे नहीं लगता कि यह कोड को पढ़ने में आसान बनाता है। इसके विपरीत, यह अनावश्यक है इसलिए यह केवल अव्यवस्था में कार्य करता है। मेरा 2 सी मुझे लगता है कि प्रत्येक अपने आप को। – davin

संबंधित मुद्दे

 संबंधित मुद्दे