Skip to content

Instantly share code, notes, and snippets.

@karbassi
Forked from adamnoffie/whats_this_do.js
Created May 14, 2010 23:41
Show Gist options
  • Save karbassi/401841 to your computer and use it in GitHub Desktop.
Save karbassi/401841 to your computer and use it in GitHub Desktop.
// what does this do?:
(function(){
var i = 5;
}());
// What this does is create an anonymous function then assigns a local variable 'i'
// with an integer value of 5 to it. Then it runs the function.
(function(){}); // creation of anonymous function
(function(){}()); // creation of anonymous function which is then ran.
// is it like a javascript hack equivalent to the jQuery(document).ready()? e.g.:
$(function() {
var i = 5;
});
// $(); is short-code for jQuery(document).ready(); Some use this for it's shortness,
// but it doesn't give you a no-conflict use of the $ character. This may problems
// if your project uses prototype or any other javascript library that uses $.
// Good reference: http://api.jquery.com/ready/
@adamnoffie
Copy link

Hey, thanks Karbassi! You kind of failed to answer my question, but that is entirely my fault - I failed to ask you the right question. But you gave me the stuff I needed to hit up Google successfully this time. Here is what I meant to ask, and some good answers:
http://stackoverflow.com/questions/1643321/javascript-why-the-anonymous-function-wrapper

@adamnoffie
Copy link

PS
then assigns a local variable 'i' with an integer value of 5 to it.

Wow, you are either very verbose, or think I'm a complete javascript n00b. Knowing you, I'm entirely sure it is the verbosity. ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment