javascriptobject-literaluglifyjsself-executing-function

Self Executing functions as properties of object literals


I'm minifying some code using UglifyJs and i'm having a problem. If I have a self executing function as a property of an object literal, on minification it's reduced to a function.

i.e.

var o = {
    c : 1,
    t : (function(){
        return 'this worked';
    }())
};

uglifyjs t.js > u.js

var o={c:1,t:function(){return"this worked"}()};

Presumably there is a reason why this way of doing things is not recommended. What/why is this? and also if there's a work around I would love to know, as the client code would rather use a property.

Thanks.


Solution

  • It returned the same self executing function without the surrounding brackets... so I don't see the problem...

    As a note, JSON doesn't support functions declared in it's content. You can use it, since it's still javascript code, but you cannot serialize it... etc.