-1

I have searched similar questions on this site but they dont answer my particular query.
Hey, so I was wondering why would you use:
(function(){a+b;})();
instead of:
a+b;
Its a thing I see used often in this tutorial I'm following:
Link
Edit:
"real code for better context":
(function() {var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; window.requestAnimationFrame = requestAnimationFrame; })();
Edit:
Didn't know it was called IIFE, thank you for the links.

  • There's a good explanation for using that construct, but your sample code doesn't seem to benefit from it. Can you post *real code* for better context? – Joseph Jul 22 '15 at 12:19
  • Your particular use case not covered by other answers seems fairly non-specific... – Álvaro González Jul 22 '15 at 12:19
  • Meanwhile, [Wikipedia](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression). – Álvaro González Jul 22 '15 at 12:21
  • See the explanation to Immediately Invoked Function Expression, [IIFE](http://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript). – Daniel B Jul 22 '15 at 12:22

1 Answers1

0

Just for encapsulation.For not to be visible in outer scope.

Alex Nikulin
  • 8,194
  • 4
  • 35
  • 37