简体   繁体   English

在项目中使用多个Javascript框架?

[英]Using multiple Javascript frameworks in a project?

Is it good or okay to have several frameworks in a project, or is it bad because it gets cluttered (= a mess), and the loading times maybe get's longer. 在项目中有多个框架是好还是可以,或者它是不好的,因为它变得混乱(=一团糟),加载时间可能会更长。 Does some 100 K matter really? 一些100 K真的重要吗? Or should you stick with one? 或者你应该坚持一个?

It's generally better to pick one thing and stick with it, for a number of reasons: 由于以下几个原因,选择一件并坚持下去通常会更好:

  • Fewer dependencies. 依赖性较低。
  • Lower complexity. 降低复杂性。
  • Easier to maintain. 更易于维护。
  • Faster loading times. 加载时间更快。
  • No likelihood of dependency conflicts (ie jQuery can't conflict with your other Javascript framework if you only have one). 不存在依赖冲突的可能性(例如,如果只有jQuery框架,jQuery不会与您的其他Javascript框架冲突)。
  • Lower debugging times. 调试时间更短。

It's true that an extra ~50k these days probably isn't going to kill anybody for a personal site or blog. 确实,现在额外的~50k可能不会杀死个人网站或博客的任何人。 The benefit comes when you scale to larger sizes, and all those extra 50k hits are eating into your bottom line. 当您扩展到更大的尺寸时,所有这些额外的50k点击都会影响您的利润。 But even if you're just doing this for a small site, the savings on your sanity from figuring out problems quicker will easily be worth it. 但即使你只是为一个小网站这样做,通过更快地解决问题来节省你的理智也很容易。

That said, if you just need one part of a specific piece of a Javascript framework, the larger ones are often split into logical chunks, so that you can just take the piece you need rather than the entire framework. 也就是说,如果您只需要Javascript框架的特定部分的一部分,那么较大的部分通常会被拆分为逻辑块,这样您就可以获取所需的部分而不是整个框架。 Most are rich enough these days that if framework X has a feature you want, but you're using framework Y, you can pretty much just map the source code for that feature directly into framework Y. 现在大多数都足够丰富,如果框架X有你想要的功能,但是你使用框架Y,你几乎可以直接将该功能的源代码映射到框架Y.

If you can load the javascript library from a repository where it would be cached on the first visit, then i don't really see any problem with that. 如果你可以从第一次访问时缓存它的存储库加载javascript库,那么我真的没有看到任何问题。

But, for uniformity sake i will go with one javascript library. 但是,为了一致起见,我将使用一个JavaScript库。

But, if you really have any strong reason to use two, then go ahead. 但是,如果你真的有充分的理由使用两个,那么继续吧。 As far as it is cached the first time. 就第一次缓存而言。

Just adding something to John's words, one could choose, for example, JQuery, which allows you to use it without clashing with other libraries. 只需在John的单词中添加一些内容,就可以选择,例如,JQuery,它允许您使用它而不会与其他库发生冲突。 I remember that once, I had troubles while trying prototype and mootools because I wanted some things from the former and some other from the latter, but it was long ago and maybe it was solved. 我记得有一次,我在尝试原型和mootools时遇到了麻烦,因为我想要一些来自前者和后者的其他东西,但它很久以前也许它已经解决了。

Somehow, I've found that it's easier to maintain a single library and there're a few real differences between them. 不知何故,我发现维护单个库更容易,它们之间存在一些真正的差异。 It related more to the way each one approaches to map documents and apply things to their elements, which causes differences in response time, but the goal happens to be the same. 它更多地涉及每个人处理地图文档并将事物应用于其元素的方式,这会导致响应时间的差异,但目标恰好相同。

Good luck with your choice :) 祝你好运:)

PS. PS。 If you gzip and fix etags in the stuff you offer in your webapps, the difference between loading one or two js frameworks is not reeeeaaally important -unless you're in facebook, meebo or so!-. 如果你在你的webapps中提供的东西gzip并修复etags,加载一个或两个js框架之间的区别并不重要 - 除非你在facebook,meebo左右! - 。 I've found that the yahoo! 我发现了雅虎! recommendations are helpful. 建议很有帮助。 http://developer.yahoo.com/performance/rules.html http://developer.yahoo.com/performance/rules.html

I wouldn't run two frameworks unless there was really no alternative. 除非没有其他选择,否则我不会运行两个框架。 They often aren't written to play well with others; 他们往往不是为了与他人合作而写的。 there are lots of potential ways they can cause unwanted interactions. 有很多潜在的方式可以导致不必要的互动。

For example with jQuery, a lot depends on the user-data key identifiers the library adds as attributes to element nodes. 例如,使用jQuery,很大程度上取决于库作为属性添加到元素节点的用户数据键标识符。 If you start messing with the structure of the document by cloning, adding/removing and so on from another framework (or even just plain DOM methods) then those identifiers aren't as unique as jQuery expects; 如果您通过克隆,添加/删除等来从另一个框架(甚至只是普通的DOM方法)开始搞乱文档的结构,那么这些标识符并不像jQuery期望的那样独特; it can easily get confused and end up failing to call event handlers, or tripping on errors. 它很容易混淆,最终无法调用事件处理程序或绊倒错误。 This sort of thing isn't fun to debug. 这种事情调试起来并不好玩。

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

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