Magical Anonymous Objects (MAO)

I came across some quirky looking code on snipplr.com, to the effect:

(function(a) {
    return a();
})({
    foo: function() {
        return "I'm magical!";
    },
    bar: function() {
        return "Well, I disagree!";
    }
}.foo);

What’s more, but which generally throws an error (varies depending on your UA):

setTimeout({
    run: (function(foo) {
        var bar = foo();
        return bar;
    })(function() { return "me!" })
}.run, 1000);

I guess I’m a bit late. This language feature is often used, it is commonly understood, to initialize complex arrays.

Post a Comment

You must be logged in to post a comment.