What does the following javascript code mean? I guess it’s defining a function within a function to make it look like OOP? Why the function can return multiple functions? what is the bracket at the end?
var grid_ui = function () {
function setup_data_source() {}
return {
init: function () {},
set_ds: function(rpt_headers, rpt_rows) {}
} // return
}();
The
{ }notation is called an object literal. It is same as:and
return { }returns an object.The
function () { ... }();is a self-invoking function: it creates an anonymous function them immidiately invokes it.In your code, the self-invoking function returns an object of functions, which is a namespace pattern. The value of
grid_uinow contains{ init: ..., set_ds: ... }, which is mentioned inreturn.These concepts are very difficult to explain in one SO answer, so I will provide you some links: