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 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。