K
Kenny
I found some code in a sample web page that ends like this:
.....
<script>
(function () {...good stuff here...})();
</script>
</body>
</html>
So it builds an anonymous function and immediately invokes it. Fine.
Now I see on another page:
<script>
....good stuff here...
</script>
So... what's up with that? I guess it is a two-part question:
1. What is the difference?
2. When is either preferred?
I can see maybe the anonymous function would have different scope, or
maybe avoid polluting the global namespace. I imagine it also runs
closer together in time because the whole thing gets parsed and then
dispatched all at once.
Am I getting warm?
kt
.....
<script>
(function () {...good stuff here...})();
</script>
</body>
</html>
So it builds an anonymous function and immediately invokes it. Fine.
Now I see on another page:
<script>
....good stuff here...
</script>
So... what's up with that? I guess it is a two-part question:
1. What is the difference?
2. When is either preferred?
I can see maybe the anonymous function would have different scope, or
maybe avoid polluting the global namespace. I imagine it also runs
closer together in time because the whole thing gets parsed and then
dispatched all at once.
Am I getting warm?
kt