3

I was reading facebook javascript SDK when I saw the following syntaxt that I don't understand: (function(){...}()); Does anyone know what does it do?

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({appId: 'your app id', status: true, cookie: true,
             xfbml: true});
  };
  (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
</script>
AlexStack
  • 16,766
  • 21
  • 72
  • 104
  • 1
    possible duplicate of [javascript function vs. ( function() { ... } ());](http://stackoverflow.com/questions/4806150/javascript-function-vs-function) – Felix Kling Jun 25 '11 at 15:19

1 Answers1

11

it's an anonymous function, that gets called immediately after its declaration. It's used to create local variables without clobbering up the global scope. the variable e will not be visible nor accessible outside the function block

knittl
  • 246,190
  • 53
  • 318
  • 364
  • And why.. dare I ask.. wouldn't we want `e` to be visible or accessible? Is it sort of like - _if a tree falls in the woods_...? – Alex Gray Feb 23 '12 at 23:21