javascript 不使用 jQuery 的一些经验技术原因是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5099949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are some empirical technical reasons not to use jQuery?
提问by rsp
Context:I am astounded by the number of front end developers that hack at HTML, Javascript and CSS all day long and that ignoretools like jQuery ( or other equivalent helper frameworks) and refuseto use them. I am not talking about JavaScript gurus, I am talking about in the trenches every day Joe production developers. I get a lot of arguments that are more like excuses or personal opinions that I don't think have any technical merit, I want to make sure I am not missing something.
背景:我对成天在 HTML、Javascript 和 CSS 上进行 hack 并且忽略像 jQuery(或其他等效的辅助框架)这样的工具并拒绝使用它们的前端开发人员的数量感到震惊。我不是在谈论 JavaScript 大师,我在谈论每天在战壕里的 Joe 生产开发人员。我收到很多更像是借口或个人意见的争论,我认为它们没有任何技术价值,我想确保我没有遗漏任何东西。
Question:What are some empirical technical reasons not to use jQuery?
问题:不使用 jQuery 的一些经验技术原因是什么?
I am not looking for religious or dogmatic arguments or subjective opinions "like some other framework is better", consider jQuery the straw man for all comparable frameworks in the question.
我不是在寻找宗教或教条论点或主观意见“就像其他框架更好”,将 jQuery 视为问题中所有可比框架的稻草人。
回答by rsp
Update 2015:
2015 年更新:
In this answer from 2011 I'm talking about libraries like jQuery, YUI or Prototype. Today in 2015 that reasoning is still applicable to frameworks like Angular, React or Ember. In those 4 years the technology progressed tremendously and even though I see considerably less prejudice against React or Angular than I saw against jQuery or YUI, the same kind of thinking - though to a lesser extent - is still present today.
在 2011 年的这个答案中,我谈论的是 jQuery、YUI 或 Prototype 之类的库。到了 2015 年,这种推理仍然适用于 Angular、React 或 Ember 等框架。在这 4 年里,技术取得了巨大的进步,尽管我看到的对 React 或 Angular 的偏见比对 jQuery 或 YUI 的偏见少得多,但同样的想法——尽管程度较轻——今天仍然存在。
Update 2016:
2016 年更新:
I highly recommend an article published few days ago:
- Why jQuery?by Michael S. Mikowski, author of the Single Page Web Applicationsbook
That article is basically a very detailed answer to this very question. Had it been available when I was writing the answer below - I would have definitely quoted it.
我强烈推荐几天前发表的一篇文章:
- 为什么是jQuery?作者:Michael S. Mikowski,Single Page Web Applications一书的作者
那篇文章基本上是对这个问题的非常详细的回答。如果在我写下面的答案时它可用 - 我肯定会引用它。
Original answer:
原答案:
I'll answer about jQuery but those are the same arguments that I've heard against using YUI, Prototype, Dojo, Ext and few others. Main arguments that I've heard:
我将回答有关 jQuery 的问题,但这些都是我听到的反对使用 YUI、Prototype、Dojo、Ext 和其他少数人的相同论点。我听到的主要论点:
file size, which in fact is 84.6 KB in case of jQuery 3.2.1- probably smaller than the logo on an average website and can be served from Google's CDN which is likely to be already in the cache of most of your visitors. As using jQuery always means smaller file size of your own JavaScript files, it can actually mean smallerdownload, even if not already in the browser cache.
speed- writing pure JavaScript may be faster, but writing portableJavaScript seems to be impossible for most of the people. A website that is faster but doesn't work on every popular browser is useless in the real world. Besides jQuery uses some pretty heavy optimizations to actually be pretty damn fast and keeps getting even faster with every release, so it's actually not so easy to write faster code by hand for anything other than trivial examples.(*)
"intellectual property"- a company is scared using someone else's code - while in fact jQuery is open source and free software that is used everywhere from your grandma's blog to Amazon, from Twitter to Bank of America, from Google to Microsoft - if they can use it then any company can use it.
I can't remember hearing any other argument being used seriously.
文件大小,实际上在jQuery 3.2.1 的情况下为 84.6 KB - 可能比一般网站上的徽标小,并且可以从 Google 的 CDN 提供,它可能已经在大多数访问者的缓存中。由于使用 jQuery 总是意味着您自己的 JavaScript 文件的文件大小更小,因此它实际上可能意味着更小的下载,即使尚未在浏览器缓存中。
速度- 编写纯 JavaScript 可能会更快,但是对于大多数人来说,编写可移植的JavaScript 似乎是不可能的。一个速度更快但不适用于所有流行浏览器的网站在现实世界中毫无用处。此外,jQuery 使用了一些非常繁重的优化,实际上非常快,并且在每次发布时都会变得更快,因此除了琐碎的示例之外,手动编写更快的代码实际上并不容易。(*)
“知识产权”——一家公司害怕使用别人的代码——而事实上 jQuery 是开源和免费软件,从你奶奶的博客到亚马逊,从 Twitter 到美国银行,从谷歌到微软——如果可以的话使用它然后任何公司都可以使用它。
我不记得听到任何其他被认真使用的论点。
(*) Here's a trivial example: getElementById('someid') vs. jQuery('#someid')
(*) 这是一个简单的例子:getElementById('someid') vs. jQuery('#someid')
Is using getElementById faster? Yes. And of course everyone always checks the parentNode to catch when Blackberry 4.6 returns nodes that are no longer in the document, right? jQuery does. And everyone handles the case where IE and Opera return items by name instead of ID, right? jQuery does. If you don't do it then your code is not portable and you introduce subtle bugs that can be very difficult to find. And getElementById is the most trivial example that one could possibly find - don't even get me started on events and AJAX and the DOM...
使用 getElementById 更快吗?是的。当然,当 Blackberry 4.6 返回不再存在于文档中的节点时,每个人都会检查 parentNode 以进行捕获,对吗?jQuery 可以。而且每个人都会处理IE和Opera按名称而不是ID返回项目的情况,对吗?jQuery 可以。如果您不这样做,那么您的代码就不可移植,并且您会引入难以发现的细微错误。getElementById 是人们可能找到的最简单的例子——甚至不要让我开始研究事件、AJAX 和 DOM ......
Update:
更新:
There is actually a fourth result of asking why someone doesn't want to use jQuery. I forgot to put it on this list because it is not really an answer but rather the lackof any answer. The commentI got yesterday reminded me about it. This is hardly a "technical reason" to be added to the list but may be interesting nonetheless and may actually be themost common reaction.
询问为什么有人不想使用 jQuery 实际上还有第四个结果。我忘了把它放在这个清单上,因为它不是真正的答案,而是缺乏任何答案。我昨天收到的评论让我想起了它。这几乎不是要添加到列表中的“技术原因”,但可能仍然很有趣,实际上可能是最常见的反应。
What I personally suspect to be the main underlying reasonto all of those reactions, though, is what I believe to be the biggest obstacle to progress in computer science: "I don't want to use it because I never did, therefore it must not be that important."
不过,我个人怀疑所有这些反应的主要原因是我认为计算机科学进步的最大障碍:“我不想使用它,因为我从未使用过,因此它必须没那么重要。”
It was once the reaction to optimizing assemblers, compilers, structured programming, higher level languages, garbage collection, object oriented programming, closures or pretty much everything that we now take for granted — and today it's AJAX libraries. Maybe some day no one will remember that we once used to manually interact with the raw DOM API on the application level like now no one remembers that we once used to write programs using raw, unadorned, inscrutable hexadecimal numbers.
它曾经是对优化汇编器、编译器、结构化编程、高级语言、垃圾收集、面向对象编程、闭包或我们现在认为理所当然的几乎所有东西的反应——今天它是 AJAX 库。也许有一天没有人会记得我们曾经在应用程序级别上手动与原始 DOM API 交互,就像现在没有人记得我们曾经使用原始的、朴素的、难以理解的十六进制数字编写程序。
回答by philwinkle
jQuery expresses everythingin a DOM-centric paradigm which can be misleading and doesn't require any need to express things in an application pattern.
jQuery在以 DOM 为中心的范式中表达一切,这可能会产生误导,并且不需要在应用程序模式中表达任何东西。
Many developers wind up programming themselves into a corner with this DOM-centric pattern and eventually realize they haven't created anything extensible or reusable.
许多开发人员最终用这种以 DOM 为中心的模式将自己编程到了一个角落,并最终意识到他们没有创建任何可扩展或可重用的东西。
Rebecca Murphey has a great write-up of her own switch to Dojo from jQuery- the blog post is more about why notjQuery versus whyDojo.
Rebecca Murphey 有一篇关于她自己从 jQuery转向Dojo的精彩文章 - 博客文章更多地是关于为什么不使用jQuery 与为什么使用Dojo。
回答by clockworkgeek
One reason not to use a framework - and this is an extreme edge case - is when writing embeddable code for another website, such as a banner. Arbitrarily inserting some complicated library or another will be polluting the namespace and potentially breaking someone else's site. Not that I wouldn't put it past some advertisers to try anyway, the pond-sucking scum, but I digress...
不使用框架的一个原因 - 这是一个极端的边缘情况 - 是为另一个网站编写可嵌入代码时,例如横幅。随意插入一些复杂的库或其他库会污染命名空间并可能破坏其他人的站点。不是说我无论如何都不会让一些广告商尝试,吸水池的人渣,但我离题了......
I disapprove of adding a framework when one is already present and equally capable. I see it all too often and it's my pet hate, I see it as unwarranted bloat. That is another question entirely.
当一个框架已经存在并且同样有能力时,我不赞成添加一个框架。我经常看到它,这是我讨厌的宠物,我认为它是毫无根据的膨胀。那完全是另一个问题。
Other than that I cannot think of a justified reason not to.
除此之外,我想不出不这样做的正当理由。
回答by jpea
filesize - but really, beyond that, it's an absolute god-send for cross-platform javascript and browser differences. You would have to have some very good reasons not to want it in your toolkit (or be a fundamentalist developer idiot).
文件大小 - 但实际上,除此之外,它是跨平台 javascript 和浏览器差异的绝对天赐之物。您必须有一些非常好的理由不想在您的工具包中使用它(或者成为一个原教旨主义的开发人员白痴)。
回答by alex
- They can't justify the filesize (even though it is probably less than script which doesn't utilise the abstractions provided).
- They don't want to rely on third party tools.
- Their business does not want to run any libraries (for whatever reasons).
- Their business does not want to run any JavaScript code not written by their employees.
- 他们无法证明文件大小的合理性(即使它可能小于不利用提供的抽象的脚本)。
- 他们不想依赖第三方工具。
- 他们的业务不想运行任何库(无论出于何种原因)。
- 他们的企业不想运行任何不是由他们的员工编写的 JavaScript 代码。
回答by JCOC611
- Learning:To actually code everything, and learn more. (Rather than using pre-coded stuff)
- Size:jQuery has TONS of features you might not need, why make the user download so much code if it's not going to be used?
- Alternatives:at this point, there are dozens of more powerful, well structured web frameworks out there.
- Flexibility:Although jQuery is really flexible, you might need something it doesn't provide.
- 学习:实际编码所有内容,并了解更多信息。(而不是使用预编码的东西)
- 大小:jQuery 有很多您可能不需要的功能,如果不打算使用,为什么要让用户下载这么多代码?
- 替代方案:在这一点上,有许多更强大、结构良好的 Web 框架。
- 灵活性:尽管 jQuery 非常灵活,但您可能需要它没有提供的功能。
回答by Eli
By all means, I like jQuery, but there are some reasons not to use jQuery:
无论如何,我喜欢 jQuery,但有一些理由不使用 jQuery:
- Download size/bandwidth: jQuery 1.5 is now over 200K uncompressed, for some the size of the library is too much to justify the benefit.
- Performance: Writing native JS will always be faster than abstracting it behind a library.
- Added complexity: Many people will download the entire jQuery library when really they only need a few neat features from it.
- Application Dependencies: Introducing dependencies always has its hang ups. If there is a bug in jQuery, you can debug and edit the file, but upgrading later introduces problems. You could leave the bug, but now you are dependent on jQuery's time table for a fix, not your own.
- 下载大小/带宽:jQuery 1.5 现在未压缩超过 200K,对于某些库来说,它的大小太大而无法证明其优势。
- 性能:编写原生 JS 总是比在库后面抽象它更快。
- 增加的复杂性:许多人会下载整个 jQuery 库,而实际上他们只需要一些简洁的功能。
- 应用程序依赖:引入依赖总是有问题的。如果 jQuery 有 bug,您可以调试和编辑文件,但稍后升级会引入问题。你可以留下这个错误,但现在你依赖于 jQuery 的修复时间表,而不是你自己的。
回答by jcoder
Because quite often it's just unnecessary. if all I want to do is validate some input, or hilight some field then it's just as easy to write simple javascript / dom code. And jQuery doesn't really help in such simple cases, so the question should be whyuse it?
因为很多时候它只是不必要的。如果我想做的只是验证一些输入,或者突出显示某个字段,那么编写简单的 javascript/dom 代码也很容易。jQuery 在这种简单的情况下并没有真正的帮助,所以问题应该是为什么要使用它?
Clearly there are many cases where it is very useful, but sometimes people seem to use it for no real reason too.
显然,在很多情况下它非常有用,但有时人们似乎也无缘无故地使用它。
回答by paul
I would prefer to use jquery for dom manipulation or traversing the dom , which is really easy with jquery . Moreover, attaching an event or event delegation are so easy using jquery or other framework otherwise you have to write custom event attachment for IE or non IE browsers etc.
我更喜欢使用 jquery 进行 dom 操作或遍历 dom ,这对 jquery 来说真的很容易。此外,使用 jquery 或其他框架附加事件或事件委托非常容易,否则您必须为 IE 或非 IE 浏览器等编写自定义事件附件。
But it has some performance penalty when you use $.each instead of vanilla JS for and array.push()... other issues like if you bind an event and remove that without unbind it will have memory leak....
但是当你使用 $.each 而不是 vanilla JS for and array.push() 时,它会有一些性能损失......其他问题,比如如果你绑定一个事件并在没有取消绑定的情况下删除它会导致内存泄漏......
My conclusion is only use any framework for complex dom manipulation and rest use vanilla JS
我的结论是只使用任何框架进行复杂的 dom 操作,其余使用 vanilla JS
回答by ClosureCowboy
Why not use jQuery?
为什么不使用jQuery?
I can't think of a good excuse to use vanilla JavaScript over jQuery (aside from the intimidation factor of learning something new), but some people prefer other JavaScript frameworks (like the excellent MooTools) due to the philosophical differences between them.
我想不出一个很好的借口来使用 vanilla JavaScript 而不是 jQuery(除了学习新东西的恐吓因素),但由于它们之间的哲学差异,有些人更喜欢其他 JavaScript 框架(如优秀的MooTools)。
Some people simply don't like jQuery's DSL-ish syntax, but they recognize the importance of using a robust JavaScript framework.
有些人只是不喜欢 jQuery 的DSL语法,但他们认识到使用健壮的 JavaScript 框架的重要性。
Personally, I lovejQuery, but I know people who use other frameworks and are no less productive.
就我个人而言,我喜欢jQuery,但我认识使用其他框架并且工作效率同样低的人。

