What are the outer parentheses in Lightning controllers?

In every Lightning controller you have these outer parentheses wrapping the actual object:

({
    getInput : function(cmp, evt) {
        var myName = cmp.find("name").get("v.value");
        var myText = cmp.find("outName");
        var greet = "Hi, " + myName;
        myText.set("v.value", greet);
    }
})

So okay – this is making it an expression but what’s the point? Somebody claimed that this is an IIFE but I don’t think so, even though “normal” JavaScript libraries are doing it with an IIFE like this:

({
    ...
})();

Why is it wrapped in parentheses but missing the following parentheses, making it an IIFE? Why is it different to “normal” JavaScript libraries? What am I missing here?

I’m not doing JavaScript all day, so it’s probably just something simple that I miss. I hope somebody can explain this.

Answer

This choice was made for two reasons.

  1. We parse the Controller and Helper on the server to validate them. There are many different Java Parsers of JSON we could use, but regardless which we choose we wanted to be consistent with the client. Which wants Objects wrapped in parentheses JSON.parse(“({})”) vs JSON.parse(“{}”).

  2. Some editors will complain that what you’re writing is not valid JSON and thus show syntax errors if you do not wrap it in ()

This decision was made many years ago (8ish), but I’m pretty sure #2 was the primary reason.

Attribution
Source : Link , Question Author : Semmel , Answer Author : Kris Gray

Leave a Comment