简体   繁体   English

为什么人们必须在同一页面上使用多个版本的jQuery?

[英]Why do people have to use multiple versions of jQuery on the same page?

I have noticed that sometimes people have to use multiple versions of jQuery in the same page (See question 1 and question 2 ). 我注意到有时人们必须在同一页面中使用多个版本的jQuery(参见问题1问题2 )。 I assume people have to carry old versions of jQuery because some pieces of their code is based on an older version of jQuery. 我假设人们必须携带旧版本的jQuery,因为它们的一些代码基于旧版本的jQuery。 Obviously, this approach causes inefficiency. 显然,这种方法会导致效率低下。 The ideal solution is to refactor the old code to use the newer jQuery API. 理想的解决方案是重构旧代码以使用更新的jQuery API。 I wonder if there are tools that automate the process of upgrading a piece of code to use a newer version of jQuery. 我想知道是否有工具可以自动升级一段代码以使用更新版本的jQuery。 I've never written programs in in either Javascript or jQuery. 我从来没有用Javascript或jQuery编写程序。 So, I'd like to hear from programmers experienced in these language about their opinion on this issue. 所以,我想听听有这些语言经验的程序员关于他们对这个问题的看法。 In particular, I'd like to know the following. 特别是,我想知道以下内容。

  1. How much of problem it is to have to load multiple versions of jQuery? 加载多个版本的jQuery需要多少问题?
  2. Have you ever had to load multiple versions of any other library in the same page? 您是否曾经在同一页面中加载任何其他库的多个版本?
  3. Do you know of any refactoring tool that helps you migrate your code to use the updated API? 您是否知道任何可以帮助您迁移代码以使用更新的API的重构工具?
  4. Do you think such a refactoring tool is useful? 你认为这样的重构工具是否有用? Are you willing to use it? 你愿意用吗?

Please don't consider this question closed. 请不要认为这个问题已经结束。 Current responses have already gave me good insight about the process and tools available for upgrading jQuery code. 目前的回复已经让我对可用于升级jQuery代码的过程和工具有了很好的了解。 But, please feel free to add your own experience with upgrading jQuery code. 但是,请随意添加自己的升级jQuery代码的经验。

your questions are all big. 你的问题都很重要。 But from my personal experience except from very little problems in the first version which happened after migrating to later ones. 但是根据我的个人经验,除了第一个版本中的一些问题,这些问题在迁移到后来的版本之后发生。 Pretty much you should not run into any problems unless something is wrong with the code you have written and not jquery. 几乎你不应该遇到任何问题,除非你写的代码有问题而不是jquery。

I have not seen any proper js developer who uses two different versions at the same time. 我没有看到任何合适的js开发人员同时使用两个不同的版本。

JQuery now itself offers a pretty nice api for unit testing called qunit. JQuery现在本身为称为qunit的单元测试提供了一个非常好的api。 and if you start implementing test functions there you should be certain that your code works seamlessly. 如果你开始实现测试功能,你应该确保你的代码无缝地工作。

I Use QUnit for heavy projects. 我将QUnit用于繁重的项目。 which requires maybe alot of ajax requests etc. ofcourse it is not really worth it to use it for couple of animations etc. 这可能需要很多ajax请求等等。当然,将它用于几个动画等并不值得。

I hope this information is helpful 我希望这些信息对你有用

Ignoring the fact that you can do this with JQuery, using 2 versions of any API is unusual and error prone. 忽略可以使用JQuery执行此操作的事实,使用任何API的2个版本是不寻常的并且容易出错。 It is a rare practice, but at least with JQuery, it is done. 这是一种罕见的做法,但至少对于JQuery来说,它已经完成了。

If I were evaluating a vendor's components for a new project and there was a requirement for an older library, I'd think twice before investing in that component. 如果我正在评估新项目的供应商组件并且需要更旧的库,那么在投资该组件之前我会三思而后行。 Vendors who supply tools that require old verions are one of: 提供需要旧版本的工具的供应商是以下之一:

1) Very cautious and stability minded (good) 1)非常谨慎和稳定(良好)

2) Not up to speed (not too good) 2)速度不快(不太好)

3) Low on resources (bad) 3)资源不足(坏)

4) Not motivated to update their tools because not enough people are demanding the tool (really bad) 4)没有动力更新他们的工具,因为没有足够的人要求该工具(非常糟糕)

Of course (5) could be that their tool is so good and they have such a big customer base they don't feel the need to update it. 当然(5)可能是他们的工具非常好而且他们拥有如此庞大的客户群,他们觉得不需要更新它。 That is ok as far as it goes, and eventually goes back to one of the other issues. 就目前而言,这是可以的,并最终回到其他问题之一。

I use enough 3rd party tools to know that sometimes, a tool does a job so well that I pick it anyway, but in general, I want a tool to be current for a new project. 我使用足够多的第三方工具来了解有时候,一个工具做得很好,无论如何我都会选择它,但总的来说,我想要一个工具成为新项目的最新工具。 There are a lot of "fly by night" vendors out there that just aren't maintaining their products and you don't find it out until you ask for support or new features. 有很多“夜间飞行”的供应商在那里只是没有维护他们的产品而且在你要求支持或新功能之前你找不到它。

I also use two versions of jQuery on the same page sometimes, but for a different reason. 我有时也在同一页面上使用两个版本的jQuery,但原因不同。 This is code that runs as a third-party widget on the page; 这是在页面上作为第三方窗口小部件运行的代码; in that situation you cannot count on a specific version of any libraries being present. 在这种情况下,您不能指望存在的任何库的特定版本。

If you need a library such as jQuery in such code, it's safest to load your version and do something such as myNamespace.jQ = jQuery.noConflict(true) so that you have a namespaced, separate version that you can rely on, and that is decoupled from the rest of the page. 如果在这样的代码中需要一个像jQuery这样的库,最安全的是加载你的版本并执行诸如myNamespace.jQ = jQuery.noConflict(true)这样的事情,这样你就可以拥有一个你可以依赖的命名空间的独立版本,与页面的其余部分分离。

That way, if that page changes their code, your third-party code won't be affected. 这样,如果该页面更改了代码,您的第三方代码就不会受到影响。 In this case, by eliminating possible dependencies on the library version in someone else's code, I'd argue that using multiple jQuery versions actually facilitates refactoring. 在这种情况下,通过消除其他人代码中对库版本的可能依赖性,我认为使用多个jQuery版本实际上有助于重构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM