IIAFEs?

Jorge jorge at jorgechamorro.com
Sat Jun 1 16:52:00 PDT 2013


On 02/06/2013, at 01:22, Brandon Benvie wrote:
> On 6/1/2013 3:44 PM, Jorge wrote:
>> But they're not fully interchangeable, for example I can exit a function at any point with a return, but can I exit a block at any point with a break or something? 
> 
>    block: {
>      if (true) {
>        break block;
>      }
>    }

What might happen with this is that if you concatenate a bunch of .js files that use this pattern, they might end redefining the same label (which would be an error, I guess). Wrapping it all in another block would solve that?

{
  block: {
    if (true) {
      break block;
    }
  }
}

But then... I'm not sure this is any better than an IIFE!

>> Also a function returns a value, does a block evaluate to something? In any case, I would really like to know which parenthesis or curly braces can I leave out in an immediately invocated arrow function expression, for example this: var x= (=>{ //... })(); Is it correct ES6? Is there anything else that I could rmv in that IIAFE? Thank you! 
> 
> You can only get the return value of a block using eval.

I knew that ;-P

> However you can accomplish the same task by assigning to some outside variable.
> 
>    let x;
>    block: {
>      x = 20;
>      // do some stuff
>      if (condition) {
>        break block;
>      }
>      x = 30;
>    }

But that's a bit awful, isn't it? When it's wrapped in an IIFE the code inside the block needs to know nothing about the outside var.

> Some fun stuff you can do with eval:
> 
>    console.log(eval("block: { 'a'; if (Math.random() > .5) break block; 'b' }"))

Cool :-)

Thanks,
-- 
( Jorge )();



More information about the es-discuss mailing list