I have recently started to delve deeper in to JavaScript and have come across this code construct in JQuery.
(function( window, undefined ) {
})(window)
Reading on stack overflow (and elsewhere) I have come to the conclusion that this is the same as
function foo(window, undefined) {
...
}
foo(window);
Am I correct in my assumption? If so, what are the advantages of the former? (other than confusing newbs)
What is jQuery Source Map? As the name suggests, it consists of a whole bunch of information that can be used to map the code within a compressed file back to it's original source . It is used by browser's debugger to help developers debug the minified version of script file.
jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.
jQuery code is just JavaScript code which depends on the jQuery library. So you can write it anywhere you write JavaScript code. Since it depends on a specific library, it simply needs to exist after the library is loaded (since JavaScript is parsed in the order in which it's written).
jQuery is a JavaScript file that you will link to in your HTML. There are two ways to include jQuery in a project, which is to download a local copy or link to a file via Content Delivery Network (CDN).
There are several things you need to know to make sense of it:
I hope that's clear, let me know if it is not. I learned all that from Paul Irish's video mentioned above.
This is an anonymous function. It is created and then goes out of scope, which here is the advantage. It is created and instantiated immediately. What is good about this is that it is not going to collide with any function on the global namespace, and thus will not obliterate anything you may have included on the page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With