-1

I don't understand benefit of anonymous function (or self-invoke)

(function () {
      $('#panel').css('background', 'red'); // I will invoke myself
})();

because if you want to manipulate DOM you should do it after page load finish that you should use $document.ready() intead of anonymous function. (because manipulate DOM require wait until that page ready)

and if you want to do something instantly after load specifical element, you could write script tag after that element like this

<body>
<div>foo</div>
<div id="panel">bar</div>
    <script>
       $('#panel').css('background', 'red');
    </script>
<div>some content</div>
</body>

so what is anonymous function benefit? please tell me thanks.

coder_rebirth
  • 57
  • 1
  • 9
  • 5
    What "anonymous function" are you referencing? The call to `.css()`? – guest271314 Sep 24 '17 at 16:38
  • There is no function in your code. – lilezek Sep 24 '17 at 16:39
  • sorry i edit it – coder_rebirth Sep 24 '17 at 16:39
  • @lilezek `.css()` is a function call – guest271314 Sep 24 '17 at 16:40
  • A `function call`, and `anonymous function` are two different things. They only have in common the word function. – lilezek Sep 24 '17 at 16:42
  • you didn't read my text? "and if you want to do instantly after some element you could write tag after that element like this" I mean, I can do something other and don't need the anonymous function – coder_rebirth Sep 24 '17 at 16:42
  • @lilezek Your comment states "There is no function in your code.", which was not accurate at the time of the comment relevant to the code at Question. – guest271314 Sep 24 '17 at 16:43
  • 1
    to make all variables private. learn more about `scope` – alen Sep 24 '17 at 16:44
  • @guest271314 $("...") is another function call. So what? – lilezek Sep 24 '17 at 16:45
  • 1
    _"I don't understand benefit of anonymous function (or self-invoke)"_ Then why would you include the IIFE in your code if you do not need to? – guest271314 Sep 24 '17 at 16:45
  • @guest271314: If he doesn't understand the benefit, then he doesn't understand when he does or does not need to include it. – llama Sep 24 '17 at 16:46
  • @lilezek Only responded to your initial comment due to the reason described at previous comment. – guest271314 Sep 24 '17 at 16:46
  • @llama There is no need to use an IIFE pattern given how the most recent edit of code at Question – guest271314 Sep 24 '17 at 16:48
  • @guest271314: Right, and the answer is that there is no benefit given that example, but there is benefit in other situations. Knowing *why* an IIFE is needed will help the OP understand when to use it. – llama Sep 24 '17 at 16:50
  • @llama Currently there are no "other situations". All that we have to reference is the actual code at the text of the Question – guest271314 Sep 24 '17 at 16:57
  • @guest271314: We also have knowledge beyond what the OP provided. We can't expect him to provide a code example relevant to when an IIFE is needed if he doesn't know when an IIFE is needed. – llama Sep 24 '17 at 17:04

1 Answers1

0

I don't understand benefit of anonymous function (or self-invoke) because if you want to manipulate DOM you should do it after page load finish that you should use $document.ready() intead of anonymous function. (because manipulate DOM require wait until that page ready)

If you're using a callback that runs when the DOM is ready, then no, you don't need another scope creation. It would be redundant.

However, that's not always the case. Sometimes people don't use an event handler to run their initializing code, so then they may use an IIFE to prevent pollution of the global variable space. If you're not creating any variables, as in your simple example, then there's no global pollution.

llama
  • 2,535
  • 12
  • 11
  • The global variable space is shared between all scripts. If you create a global variable in your script, and another script loads, it may overwrite your variable. In older versions of JS, the only way to scope a variable locally was inside a function. So a function would be created and executed immediately so that any `var` declarations inside would *stay* inside, and not become globals. – llama Sep 24 '17 at 16:52
  • Thanks you made my day : ) – coder_rebirth Sep 24 '17 at 16:58