最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Should I use IIFE or window onload to initialize? - Stack Overflow

programmeradmin10浏览0评论

Both of the following code snippets worked:

Using IIFE in js file:

(function initialize() {
  txtInput = document.getElementById('txtInput');
  txtResult = document.getElementById('txtResult');

  txtInput.value = "0";
  txtResult.value = "0";

}());

Calling initialize() on window load event in html file:

window.addEventListener('load', initialize, false);

Is one a better approach than other; in terms of performance or otherwise? As it stands right now, I am leaning more towards adding event listener to the window object, because it is more readable.

Both of the following code snippets worked:

Using IIFE in js file:

(function initialize() {
  txtInput = document.getElementById('txtInput');
  txtResult = document.getElementById('txtResult');

  txtInput.value = "0";
  txtResult.value = "0";

}());

Calling initialize() on window load event in html file:

window.addEventListener('load', initialize, false);

Is one a better approach than other; in terms of performance or otherwise? As it stands right now, I am leaning more towards adding event listener to the window object, because it is more readable.

Share Improve this question edited Jul 11, 2021 at 21:51 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 20, 2014 at 15:10 GMallaGMalla 1911 silver badge7 bronze badges 4
  • 4 it's window.addEventListener('load', initialize, false); -> will be executed on dom content loaded. – lshettyl Commented Mar 20, 2014 at 15:17
  • Not really, it will call initialize when all resources has loaded, with 3rd party stuff the difference can be huge. – anddoutoi Commented Mar 20, 2014 at 15:25
  • 1 It all depends on if you intend to run this before or after the elements are actually added. If you are sure that they exist - use the IIFE. – Johan Commented Mar 20, 2014 at 15:30
  • @LShetty oops, fixed the typo. – GMalla Commented Mar 20, 2014 at 15:49
Add a comment  | 

3 Answers 3

Reset to default 12

It depends when you want the code to run. If you want the code to execute ASAP you can use an IIFE but there is really no point using an IIFE if you don't use it to protect your variables and/or not polluting the global scope.

(function initialize() {
    // do somthing
}());

or

// do somthing

will execute at the same point in time.

If you want to defer execution there are three points in time usually used by web devs. <script>s at bottom, DOMContentLoad and window.onload.

  • <script>s at bottom will execute after they are fetched from the server.
  • DOMContentLoaded basicly execute as soon as </html> has been read by the HTML parser.
  • very simplified window.onload executes after all CSS, <img>es and <script>s have been loaded.

Note that in reality, with attributes like async and defer on <script>s, this is more complex, . This is why there is a mountain of resource loaders available.

Probably the result of each approach matters, not the performance. The first approach runs immediately while the second one waits until dom is ready. The end result is that in your first approach, you may end up getting undefined for both textInput and textResult, unless you place the script on the bottom of page.

The IIFE in a script element (whether inline or external loaded) just before the closing body element certainly appears most clever. It confuses the hell out of commoners.

document.addEventListener('DOMContentLoaded', function() ... is easy to understand. It's almost plain English: event listens for DOM content loaded. So, poof, the majesty is gone. Note this is distinct from window onload.

I use the event listener, as it adheres to the KISS principle, it's a purpose built tool, and it does what it says it does (which probably includes stuff/functionality I haven't even considered).

发布评论

评论列表(0)

  1. 暂无评论