Implications of multiple [removed] tags in HTML

前端 未结 5 588
野性不改
野性不改 2020-12-08 09:54

I have read that it is not recommended to instantiate jQuery multiple times in your HTML. This makes perfect sense to me, but: Isn\'t Javascript single-threaded anyway? And

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 10:16

    The fewer calls you make that instantiate a jQuery object, the less overhead you have -- but even if you are designing for old browsers running on 2nd generation hardware be wary of micro-optimizations. Profile your application and fix the parts that actually are the bottlenecks.

    As for the way browsers handle multiple script tags -- it varies from browser to browser, from version to version, and sometimes even from operating system to operating system. All browsers execute each script tag in document order:

     
     
     
     
    

    However, other behaviors are not defined and there is variation. For example, Opera (at least on Windows XP for version 10.6) ran each script tag in its own context -- so local variables in the 3rd script block would be out of scope if referred to in the 4th script block.

    
    
    

提交回复
热议问题