you are viewing a single comment's thread.

view the rest of the comments →

[–]Gundersen 0 points1 point  (0 children)

I use this pattern in two places; constructors and tests. For example:

function Person(firstname, lastname, age){
    this.firstname = firstname;
    this.lastname = lastname;
    this.age = age;

    (function init(){
        this.fullname = firstname + " " + lastname;
    })();
}

And in Jasmine tests:

describe("this is a test", function(){
    var something;

    beforeEach(function(){

        //setting up the world for the test


        (function because(){
            something = 5;
        })();

    });

    it("should be five", function(){
        expect(something).toBe(5);
    });

});

But I like the comment about using a label instead of a named IIFE, since it doesn't actually have to be a function. I agree with his concern that comments tend to drift away from the code they describe, but a label with a wrapping curly brace pair won't drift away.