functions in javascript wrapped in parenthesis

I am new to javascript and I might have dove into the deep end first. I have came across the following definition while reading design patterns in js. I don’t understand the syntax used here, why is the “log” function definition in “()”,

var log = (function() {
    var log = "";
    return {
        add: function(msg) { log += msg + "n"; },
        show: function() { alert(log); log = ""; }
    }
})();

Please point me in the right direction.


Source: javascript

Leave a Reply