Is there a benefit to using a return statement that returns nothing?

前端 未结 5 653
滥情空心
滥情空心 2020-12-13 02:00

I\'m refactoring a large javascript document that I picked up from an open source project. A number of functions use inconsistent return statements. Here\'s a simple examp

相关标签:
5条回答
  • 2020-12-13 02:36

    Changing your functions will actually alter the code because return; and return false; output different data types.

    var test = function (x) {
        if (!x) {
            return;
        }
        else {
            return false;
        }
    };
    
    var a = test(true), b = test(false);
    
    console.log(typeof b); // undefined
    console.log(typeof a); // boolean
    
    0 讨论(0)
  • 2020-12-13 02:38

    Using return without a value will return the value undefined.

    If the value is evaluated as a boolean, undefined will work as false, but if the value for example is compared to false, you will get a different behaviour:

    var x; // x is undefined
    alert(x); // shows "undefined"
    alert(!x); // shows "true"
    alert(x==false); // shows "false"
    

    So, while the code should logically return true or false, not true or undefined, you can't just change return; to return false; without checking how the return value is used.

    0 讨论(0)
  • 2020-12-13 02:43

    What MIGHT be lost here (not direct with your example) is that you can then have a tri-state object:

    var myfunc = function(testparam) {
        if (typeof testparam === 'undefined') return;
        if (testparam) {
            return true;
        }
        else {
            return false;
        }
    };
    
    var thefirst = myfunc(true)
    var thesecond = myfunc(false);
    var thelast = myfunc();
    alert("type:" + typeof thefirst+" value:"+thefirst);
    alert("type:" + typeof thesecond+" value:"+thesecond);  
    alert("type:" + typeof thelast+" value:"+thelast); 
    

    these return:

    > type:boolean:true 
    > type:boolean:false
    > type:undefined:undefined
    

    note: null would return false in this example myfunc(null);

    0 讨论(0)
  • 2020-12-13 02:46

    "Blank return" statements can be used to transfer the control back to the calling function (or stop executing a function for some reason - ex: validations etc). In most cases I use blank return statement is when I'm doing some kind of a validation. However, I make it a point to set some indicator as to why the execution of the function is stopped. For example, set the "innerText" property on a DIV element with the error message.

    In the code above, it looks like it is a validation. The function returns a "true" if everything went well. It looks like the calling function parses the return value, and if it is "true", next step of statements (in the calling function) are executed.

    It is a good practice to return "false" instead of a blank return in the above example. That way you make it all uniform and make life easy for other programmers.

    You could fix such inconsistencies; however, make sure you test all the changes thoroughly. It is a good practice to test each change you make to the code, however small it may be.

    0 讨论(0)
  • 2020-12-13 02:48

    There is no difference at all between return; and return undefined;. The result of calling both functions is to receive the value undefined.

    (There's a very small specification-level difference between a function body that terminates with return vs. just falling off the end of the code, but it's nothing that can be detected in code.¹ Calling a function where execution falls off the end of the code also results in the value undefined.)

    "use strict";
    
    // Implicit return of `undefined`
    function x() {
        return;
    }
    // Explicit return of `undefined`
    function y() {
        return undefined;
    }
    // Execution falls off the end
    function z() {
    }
    console.log(typeof x() === "undefined"); // true
    console.log(typeof y() === "undefined"); // true
    console.log(typeof z() === "undefined"); // true


    Unless, of course, something has shadowed undefined. Which is still sadly possible (though not, gladly, at global scope). In that very edgy edge case, there's a difference:

    "use strict";
    (function() {
        const undefined = 42;
    //  ^^^^^^^^^^^^^^^---- shadowing `undefined`
        // Implicit return of `undefined`
        function x() {
            return;
        }
        // Explicit return of `undefined`
        function y() {
            return undefined;
        }
        // Execution falls off the end
        function z() {
        }
        console.log(typeof x() === "undefined"); // true, `x` returns the canonical `undefined`
        console.log(typeof y() === "undefined"); // false, `y` returns 42
        console.log(typeof z() === "undefined"); // true, `z` (effectively) returns the canonical `undefined`
    })();


    ¹ Using return is an abrupt completion that [[Call]] converts to a normal completion w/value. Falling off the end of the code is a normal completion (spec) (that [[Call]] ensures supplies undefined for the value). But again, this is a specification level difference, not something that's observable in code.

    0 讨论(0)
提交回复
热议问题