duanke8011 2013-10-28 10:03
浏览 65
已采纳

在构建网站时,我应该在特定用户控件或母版页中引用javascript库吗?

which of following cases is strongest with regard to client side (JavaScript) library referencing and loading.

Please note that I assume the web solution is well designed so that the components are well encapsulated

Also, just to be clear, the master page I am talking about could be a user control or anything that holds all library references and loaded at the site-load regardless of the content of the site at the moment

1 - include references (link href) for all custom (e.g. 3rd party) libraries in the master page.

  • it's arguable this is easy to maintain because all references are in one place.

  • it's also arguable that it's easy to perform library upgrades so you don't need to hunt within the solution where the library is referred to update it's reference.

2 - include reference to libraries in their specific user controls

  • shouldn't this improve performance as the website only loads libraries when required. e.g. one can refer to the JavaScript libraries that works for maps within the user control that deals with maps with contrast to loading maps libraries even when there's no map on the current view.

  • This encapsulates components well. for example, a developer who tries to fix a bug in the map component will see all the libraries it refers to when he opens the map user control. This stops the need to go through the master page and see which libraries are used.

  • 写回答

1条回答 默认 最新

  • douye2572 2013-10-28 10:18
    关注

    I don't think this is the right way to think about it.

    The only reason not to load all libraries is performance. The debug reason you give is too minor to consider.

    You can increase performance in many different ways. By far the easiest, most straightforward and attainable is to minify all your JavaScript into one file. You can additionally use a compiler like Google's Closure Compiler or Yahoo YUI Compressor to get a smaller filesize and faster execution.

    If at this point you still have performance issues (that can be attributed to network latency) then maybe look into lazy loading your libraries.

    This doesn't mean you should forgo the guidelines of dependency injection. Any good framework will offer you a mechanism for dependency injection. This should make switching to solution 2 at a later point fairly trivial.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题