問題描述
背景:我對成天在 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 和其他少數幾個的論點相同.我聽到的主要論點:
文件大小,在 jQuery 3.2.1 - 可能比一般網站上的徽標小,可以從 Google 的 CDN 提供服務,該 CDN 可能已經在大多數網站的緩存中您的訪客.由于使用 jQuery 總是意味著您自己的 JavaScript 文件的文件大小更小,它實際上可能意味著 更小 下載,即使尚未在瀏覽器緩存中.
速度 - 編寫純 JavaScript 可能會更快,但編寫 portable JavaScript 對大多數人來說似乎是不可能的.一個速度更快但不能在所有流行瀏覽器上運行的網站在現實世界中毫無用處.此外,jQuery 使用了一些非常重的優化,實際上速度非常快,并且在每個版本中都變得越來越快,因此除了瑣碎的示例之外,手動編寫更快的代碼實際上并不是那么容易.(*)
知識產權" - 公司害怕使用別人的代碼 - 而事實上 jQuery 是開源和免費軟件,從你奶奶的博客到亞馬遜,從從 Twitter 到美國銀行,從 Google 到微軟——如果他們可以使用它,那么任何公司都可以使用它.
我不記得聽到過任何其他被認真使用的論點.
(*) 這是一個簡單的例子: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:
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.
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.(*)
"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.
(*) 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.
這篇關于什么是不使用 jQuery 的經驗技術原因?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!