什么是不使用 jQuery 的经验技术原因?

2022-01-16 00:00:00 frameworks jquery javascript

背景:我对成天在 HTML、Javascript 和 CSS 上进行攻击的前端开发人员的数量感到震惊,并且 忽略 像 jQuery( 或其他等效的辅助框架)和 ignore 工具.strong>拒绝使用它们.我不是在谈论 JavaScript 大师,我是在谈论每天在战壕中的 Joe 生产开发人员.我收到很多争论,这些争论更像是借口或个人意见,我认为这些观点没有任何技术价值,我想确保我没有遗漏什么.

问题:什么是不使用 jQuery 的经验技术原因?

我不是在寻找像其他框架更好"的宗教或教条论据或主观意见,将 jQuery 视为问题中所有可比框架的稻草人.

解决方案

2015 年更新:

<块引用>

在 2011 年的这个答案中,我谈论的是 jQuery、YUI 等库或原型.2015 年的今天,这种推理仍然适用于Angular、React 或 Ember 等框架.在那4年里技术进步很大,即使我看到了很多对 React 或 Angular 的偏见比我对 jQuery 或YUI,同样的想法——尽管程度较小——仍然存在今天.

2016 年更新:

<块引用>

强烈推荐几天前发表的一篇文章:

  • 为什么选择 jQuery? 作者:Michael S. Mikowski,单页Web 应用程序 书

那篇文章基本上是一个非常详细的答案问题.如果在我写下面的答案时它可用 - 我肯定会引用它.

原答案:

我将回答有关 jQuery 的问题,但这些与我听到的反对使用 YUI、Prototype、Dojo、Ext 和其他少数几个的论点相同.我听到的主要论点:

  1. 文件大小,在 jQuery 3.2.1 - 可能比一般网站上的徽标小,可以从 Google 的 CDN 提供服务,该 CDN 可能已经在大多数网站的缓存中您的访客.由于使用 jQuery 总是意味着您自己的 JavaScript 文件的文件大小更小,它实际上可能意味着 更小 下载,即使尚未在浏览器缓存中.

  2. 速度 - 编写纯 JavaScript 可能会更快,但编写 portable JavaScript 对大多数人来说似乎是不可能的.一个速度更快但不能在所有流行浏览器上运行的网站在现实世界中毫无用处.此外,jQuery 使用了一些非常重的优化,实际上速度非常快,并且在每个版本中都变得越来越快,因此除了琐碎的示例之外,手动编写更快的代码实际上并不是那么容易.(*)

  3. 知识产权" - 公司害怕使用别人的代码 - 而事实上 jQuery 是开源和免费软件,从你奶奶的博客到亚马逊,从从 Twitter 到美国银行,从 Google 到微软——如果他们可以使用它,那么任何公司都可以使用它.

  4. 我不记得听到过任何其他被认真使用的论点.

(*) 这是一个简单的例子:getElementById('someid') vs. jQuery('#someid')

使用 getElementById 是否更快?是的.当然,当 Blackberry 4.6 返回不再在文档中的节点时,每个人都会检查 parentNode 以捕获它,对吗?jQuery 可以.每个人都会处理 IE 和 Opera 按名称而不是 ID 返回项目的情况,对吗?jQuery 可以.如果您不这样做,那么您的代码就不可移植,并且您会引入很难找到的细微错误.getElementById 是人们可能找到的最简单的例子——甚至不要让我开始讨论事件、AJAX 和 DOM...

更新:

实际上还有第四个结果是问为什么有人不想使用 jQuery.我忘了把它放在这个列表上,因为它不是真正的答案,而是任何答案的缺乏.评论我昨天被提醒了.这几乎不是技术原因".将被添加到列表中,但可能仍然很有趣,并且实际上可能是 最常见的反应.

我个人怀疑所有这些反应的主要原因是我认为是计算机科学进步的最大障碍:我不希望使用它,因为我从未使用过,因此它一定没那么重要."

它曾经是对优化汇编器、编译器、结构化编程、高级语言、垃圾收集、面向对象编程、闭包或几乎所有我们现在认为理所当然的东西的反应——而今天它是 AJAX 库.也许有一天没有人会记得我们曾经在应用程序级别手动与原始 DOM API 交互,就像现在没有人记得我们曾经使用 原始、朴素、难以理解的十六进制数字.

Context: I am astounded by the number of front end developers that hack at HTML, Javascript and CSS all day long and that ignore tools like jQuery ( or other equivalent helper frameworks ) and refuse to 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.

Question: What are some empirical technical reasons not to use 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.

解决方案

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

Update 2016:

I highly recommend an article published few days ago:

  • Why jQuery? by Michael S. Mikowski, author of the Single Page Web Applications book

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.

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:

  1. 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 smaller download, even if not already in the browser cache.

  2. speed - writing pure JavaScript may be faster, but writing portable JavaScript 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.(*)

  3. "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.

  4. I can't remember hearing any other argument being used seriously.

(*) Here's a trivial example: 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...

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 lack of any answer. The comment I 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 the most common reaction.

What I personally suspect to be the main underlying reason to 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.

相关文章