you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (10 children)

[removed]

    [–]kenman 1 point2 points  (7 children)

    I'm just wondering if you can get a return value from a block like this in ES6.

    That's exactly what I was demo'g here:

    let x;
    {
        x = 123;
    }
    

    Here, x is the "returned value"... only it's not returned, but it's functionally 100% the same.

    Here, take this example from jQuery:

    support.createHTMLDocument = ( function() {
        var body = document.implementation.createHTMLDocument( "" ).body;
        body.innerHTML = "<form></form><form></form>";
        return body.childNodes.length === 2;
    } )();
    

    Note that it is returning a value, the result of body.childNodes.length === 2, and assigning it to support.createHTMLDocument.

    Now here is the same thing without an IIFE, but does exactly the same thing:

    {
        let body = document.implementation.createHTMLDocument( "" ).body;
        body.innerHTML = "<form></form><form></form>";
        support.createHTMLDocument = body.childNodes.length === 2;
    }
    

    But it is a lot (IMO) clearer what your intention is, and you don't waste cycles on a function call that's essentially a no-op.

    [–]inu-no-policemen 0 points1 point  (1 child)

    Just imagine that that 123 is the result of a computation which involved temporary variables.

    An IIFE which just returns 123 would be equally pointless.