I’ve been digging into jquery to find out how it works and I see it uses a construct i’ve never seen in JS before. The following code seems to execute when the browser loads, it’s almost like a function that invokes itself. I’ve searched for docs for this feature but not sure what its called. Can someone tell me the principle so I can google further info on this?
(function test() {
alert('test');
})();
That’s exactly what it is, a self-invoking anonymous function, the variables inside that scope:
Won’t be visible outside unless you expose them either. If you want more detail around uses and practical examples, I’d start with this question.