parse.com - what is this Javascript opening syntax? -
i'm doing tuts tutorial on intel xdk , parse , 1 of source files has syntax i've never seen anywhere. file opens function not has no name, declared inside regular parentheses. can explain and/or link online resource explains this?
(function (credentials) { var exports = {}; var baseurl = 'https://api.parse.com/1/classes/article/'; exports.articles = function (params) { params = params || {}; var artid = ''; if (params.id) { artid = params.id; } var url = baseurl + artid; return $.ajax({ url: url, headers: { 'x-parse-application-id' : credentials.apikey, 'x-parse-rest-api-key' : credentials.apisecret } }); }; return exports; })
your code snippet many pointed out, missing pair of () @ end. (), becomes iife, wikipedia article pointed mike explains clearly.
in brief, immediately-invoked function expression executed once program encounters it. consider simple case below:
//your awesome js console.log(b); // undefined (function() { b = "cat"; })(); console.log(b); // cat. since in above iife, defined global variable b.
you can pass parameters iife this:
(function(input) { console.log(input); // 5 })(5);
your code above creates "export" object , returns it. if read "establishing private variables , accessors" section in wiki, you'll see how used nicely create "private" variables.
Comments
Post a Comment