Using multiple Javascript frameworks in a project?

核能气质少年 提交于 2021-02-07 08:44:34

问题


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? Or should you stick with one?


回答1:


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 (i.e. jQuery can't conflict with your other Javascript framework if you only have one).
  • 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. The benefit comes when you scale to larger sizes, and all those extra 50k hits are eating into your bottom line. 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. 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.




回答2:


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.

But, for uniformity sake i will go with one javascript library.

But, if you really have any strong reason to use two, then go ahead. As far as it is cached the first time.




回答3:


Just adding something to John's words, one could choose, for example, JQuery, which allows you to use it without clashing with other libraries. 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.

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. 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!-. I've found that the yahoo! recommendations are helpful. http://developer.yahoo.com/performance/rules.html




回答4:


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. 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; 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.



来源:https://stackoverflow.com/questions/1963828/using-multiple-javascript-frameworks-in-a-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!