Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding jquery source code

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)

like image 868
My name is Richard Avatar asked Oct 02 '10 16:10

My name is Richard


People also ask

What is jQuery source?

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.

What are the basic concepts of jQuery?

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.

Where is jQuery code written?

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).

Does jQuery go in HTML or JS?

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).


2 Answers

There are several things you need to know to make sense of it:

  1. It is an anonymous function, which simply means it does not have a name.
  2. The function is called immediately after it is declared. You see the open parenthesis on line 2, immediately after the function definition? That means, "call this function".
  3. Only one parameter is passed to the function. That parameter is "window", which is the name of the global scope inside of a browser.
  4. The function being called actually expects *2* parameters, but we're calling it with one. Javascript lets you call functions with more or fewer parameters than the function actually expects. There are ways of getting to the list of parameters that was passed.
  5. Since we are only passing one parameter, the second parameter will automatically be set to "undefined". "undefined" is a special javascript value that means, get ready, "undefined".
  6. It just so happens that we have also named our second parameter with the name "undefined". So in effect, we have created a local variable (parameters are very much like local variables) that is named undefined, and whose value is undefined.
  7. Why on Earth did we do that? It is a way of ensuring that, within our anonymous function, if we refer to "undefined", it really will have the value of "undefined". If we didn't do that, and some crazy code outside of our scope redefined "undefined" (by saying something like "undefined = 42"), then we'd write code thinking we were referring to undefined but we'd actually be referring to 42. These shenanigans with passing 1 parameter but expecting 2, and calling the second one undefined, protects us from such nonsense.

I hope that's clear, let me know if it is not. I learned all that from Paul Irish's video mentioned above.

like image 94
Charlie Flowers Avatar answered Sep 24 '22 23:09

Charlie Flowers


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.

like image 45
Scott Avatar answered Sep 20 '22 23:09

Scott