I have recently been thinking alot about where to draw the line, and I wanted to get some answers from you experienced developers out there. When is it acceptable to reference jQuery in a web app?
我最近一直在考虑在哪里划线,我想从那里经验丰富的开发人员那里得到一些答案。什么时候可以在Web应用程序中引用jQuery?
Some supporting arguments for Always Using jQuery:
始终使用jQuery的一些支持参数:
- You can reference jQuery from Google's repository, where it might already be cached on the client, eliminating the extra hefty request
- You don't have to deal with common cross-browser issues, as they are already taken care of
- You enjoy writing the code because it is developer-friendly
您可以从Google的存储库中引用jQuery,它可能已经缓存在客户端上,从而消除了额外的大量请求
您不必处理常见的跨浏览器问题,因为它们已经被处理掉了
您喜欢编写代码,因为它对开发人员友好
Some supporting arguments for Never Using jQuery:
从不使用jQuery的一些支持参数:
- Unsettling dependency of a third-party library, which could contain costly bugs, and could lead to unjustifiably poor performance
- You have to learn a new syntax (just when you thought you had regular Javascript figured out...)
- Even the simplest of tasks can perform poorly, because of the inherent complexity of the framework
令第三方库的依赖性令人不安,这可能包含代价高昂的错误,并可能导致不合理的性能不佳
你必须学习一种新的语法(就在你认为你经常使用Javascript时...)
由于框架固有的复杂性,即使最简单的任务也可能表现不佳
I listed arguments as part of the question because I do not wish to have answers weighing the arguments of both sides. I am hoping for always/never answers, preferrably with test case scenarios (vague or detailed).
我将争论列为问题的一部分,因为我不希望得到双方争论的答案。我希望永远/永远不会回答,最好是测试用例场景(模糊或详细)。
For example: "If I ever need to do any AJAX requests in my web app, I always use jQuery".
例如:“如果我需要在我的Web应用程序中执行任何AJAX请求,我总是使用jQuery”。
10 个解决方案
#1
If I ever need to do anything past document.getElementById() in my web app, I always use jQuery.
如果我需要在我的web应用程序中通过document.getElementById()做任何事情,我总是使用jQuery。
I honestly find most of your con arguments against jQuery pretty weak, particularly the last one:
老实说,我发现大多数针对jQuery的骗局都很弱,尤其是最后一个:
Even the simplest of tasks can perform poorly, because of the inherent complexity of the framework.
由于框架固有的复杂性,即使最简单的任务也可能表现不佳。
Do not agree with that at all. jQuery is incredibly simple while powerful and that's what I love about it.
根本不同意这一点。 jQuery非常简单而强大,这就是我喜欢它。
#2
Unsettling dependency of a third-party library, which could contain costly bugs, and could lead to unjustifiably poor performance
令第三方库的依赖性令人不安,这可能包含代价高昂的错误,并可能导致不合理的性能不佳
Cumulatively, the jQuery developers have invested far more effort in eliminating bugs and performance problems on a wider variety of browsers than you or any other small team could afford to do.
累积起来,jQuery开发人员在消除各种浏览器上的错误和性能问题方面投入了更多的精力,而不是你或任何其他小团队可以负担得起的。
Not-Invented-Here syndrome is a waste of your time.
Not-Invented-Here综合症是浪费你的时间。
#3
It doesn't matter- you can write good code or rotten code, with or without jquery.
没关系 - 无论是否有jquery,你都可以编写好的代码或糟糕的代码。
#4
It's acceptable when you want extra flexibility that just isn't in the DOM. By default I do not inclue JQUERY; however, as soon as I need to do any client side lifting I am moving to JQUERY.
当你想要在DOM中没有的额外灵活性时,这是可以接受的。默认情况下,我不包括JQUERY;但是,只要我需要做任何客户端解除,我就会转向JQUERY。
I will even use JQUERY and MSAjax at the same time (Mostly on legacy pages which I am moving away from MSAjax).
我甚至会同时使用JQUERY和MSAjax(主要是在我离开MSAjax的遗留页面上)。
#5
I would say that it's acceptable to use jQuery whenever you don't want to waste time dealing with different Javascript implementations. Which is probably quite close to always..
我想说,每当你不想浪费时间处理不同的Javascript实现时,使用jQuery是可以接受的。这可能是非常接近...
If what you do is a fairly small and trivial thing introducing jQuery (19KB minified) as a dependency might be a little overkill.
如果你所做的是一个相当小而微不足道的事情,引入jQuery(19KB缩小)作为依赖可能有点矫枉过正。
Don't reinvent the wheel if you don't have to!
如果你不需要,不要重新发明*!
#6
There aren't very many things I find javascript useful for that jQuery doesn't simplify.
我发现javascript不是很多东西,因为jQuery没有简化。
For data validation, I prefer to have just one implementation of that, and since I have to make the server validate everything anyway, Might as well use AJAX for form validation. jQuery is very good at that.
对于数据验证,我更喜欢只有一个实现,因为我必须让服务器验证一切,不妨使用AJAX进行表单验证。 jQuery非常擅长。
For DOM Manipulation, jQuery provides a terse way of expressing what I'm likely to need. Certainly shorter and probably clearer than the 3 to 10 function calls I'd otherwise need.
对于DOM操作,jQuery提供了一种简洁的方式来表达我可能需要的东西。肯定比我需要的3到10个函数调用更短,可能更清晰。
Visual effects are seldom really required. They're hard to make portable too. For the benefit, if I cannot find a prepackaged visual effect, I'm probably better off without it. jQuery UI and several established plugins fit the bill.
视觉效果很少真正需要。他们也很难携带。为了好处,如果我找不到预先打包的视觉效果,没有它我可能会更好。 jQuery UI和几个已建立的插件符合要求。
There are plenty of other JavaScript Libraries, and I'm sure many of them are great, but unless they provide an absolutely indispensable function (eg, Google API), I can probably get jQuery to do it well too, and I'm already familiar with jQuery.
还有很多其他的JavaScript库,我相信它们中的很多都很棒,但除非它们提供了绝对不可或缺的功能(例如Google API),否则我可能会让jQuery做得好,而且我已经熟悉jQuery。
#7
I never use jQuery... as for why? Never really thought about it, I guess I just haven't found a reason to use anything beyond plain Javascript. Plus, I don't like to rely on layers of abstraction I don't understand. (Of course, the things I do with Javascript aren't particularly complicated, so in my case perhaps jQuery really would be more trouble than it's worth.)
我从不使用jQuery ......至于为什么?从来没有真正考虑过它,我想我还没有找到使用普通Javascript之外的任何东西的理由。另外,我不喜欢依赖于我不理解的抽象层。 (当然,我用Javascript做的事情并不是特别复杂,所以在我的情况下,jQuery真的会比它的价值更麻烦。)
#8
Today I added a Javascript patch to a page where I had to move a div out of its original place in the DOM tree and make it a child of the body, so that it could be positioned properly. I knew it would only have to happen once, on startup, to a specific id'd div, and that the page wasn't going to use any other JS behavior. So I wrote it by hand.
今天我在页面中添加了一个Javascript补丁,我必须将div从DOM树中的原始位置移出并使其成为正文的子节点,以便可以正确定位。我知道它只需要在启动时发生一次特定的id'd div,并且该页面不会使用任何其他JS行为。所以我手工写了。
For anything more complex, it's jQuery. The high-level API just saves me so much time that I would otherwise have to spend "talking down" to the computer.
对于任何更复杂的东西,它都是jQuery。高级API只是为我节省了很多时间,否则我不得不花费“低谈”到电脑上。
#9
Well, at least until EcmaScript 5 will come out and until the browsers will implement in their JavaScript Engines a common set of specifications, jQuery is the best tool that we have to solve problems cross browser. Not long ago every time you started a js file you had to start with a browser detect function. I used prototype, mootools, dojo and lately jQuery and I am very satisfied with the help they provide.
好吧,至少在EcmaScript 5问世之前,直到浏览器在他们的JavaScript引擎中实现一套通用规范,jQuery是我们必须通过浏览器解决问题的最佳工具。不久前,每次启动js文件时,都必须从浏览器检测功能开始。我使用了原型,mootools,dojo和最近的jQuery,我对他们提供的帮助非常满意。
#10
Using jQuery just for browser compatibility is not a good enough reason for me to use jQuery. Most javascript framework these days already able to solve that. As for me, I would use jQuery if I ever need a very simple DOM manipulation tool that has Ajax support. If I ever need something beyond that, I would use other javascript framework. I would use Mootools if I need Javascript Object Oriented support and I would use Ext-JS if I ever need extensive built in widgets.
仅仅为了浏览器兼容性而使用jQuery并不足以让我使用jQuery。现在大多数javascript框架已经能够解决这个问题了。至于我,如果我需要一个支持Ajax的非常简单的DOM操作工具,我会使用jQuery。如果我需要其他东西,我会使用其他javascript框架。如果我需要Javascript面向对象的支持,我会使用Mootools,如果我需要大量的内置小部件,我会使用Ext-JS。
#1
If I ever need to do anything past document.getElementById() in my web app, I always use jQuery.
如果我需要在我的web应用程序中通过document.getElementById()做任何事情,我总是使用jQuery。
I honestly find most of your con arguments against jQuery pretty weak, particularly the last one:
老实说,我发现大多数针对jQuery的骗局都很弱,尤其是最后一个:
Even the simplest of tasks can perform poorly, because of the inherent complexity of the framework.
由于框架固有的复杂性,即使最简单的任务也可能表现不佳。
Do not agree with that at all. jQuery is incredibly simple while powerful and that's what I love about it.
根本不同意这一点。 jQuery非常简单而强大,这就是我喜欢它。
#2
Unsettling dependency of a third-party library, which could contain costly bugs, and could lead to unjustifiably poor performance
令第三方库的依赖性令人不安,这可能包含代价高昂的错误,并可能导致不合理的性能不佳
Cumulatively, the jQuery developers have invested far more effort in eliminating bugs and performance problems on a wider variety of browsers than you or any other small team could afford to do.
累积起来,jQuery开发人员在消除各种浏览器上的错误和性能问题方面投入了更多的精力,而不是你或任何其他小团队可以负担得起的。
Not-Invented-Here syndrome is a waste of your time.
Not-Invented-Here综合症是浪费你的时间。
#3
It doesn't matter- you can write good code or rotten code, with or without jquery.
没关系 - 无论是否有jquery,你都可以编写好的代码或糟糕的代码。
#4
It's acceptable when you want extra flexibility that just isn't in the DOM. By default I do not inclue JQUERY; however, as soon as I need to do any client side lifting I am moving to JQUERY.
当你想要在DOM中没有的额外灵活性时,这是可以接受的。默认情况下,我不包括JQUERY;但是,只要我需要做任何客户端解除,我就会转向JQUERY。
I will even use JQUERY and MSAjax at the same time (Mostly on legacy pages which I am moving away from MSAjax).
我甚至会同时使用JQUERY和MSAjax(主要是在我离开MSAjax的遗留页面上)。
#5
I would say that it's acceptable to use jQuery whenever you don't want to waste time dealing with different Javascript implementations. Which is probably quite close to always..
我想说,每当你不想浪费时间处理不同的Javascript实现时,使用jQuery是可以接受的。这可能是非常接近...
If what you do is a fairly small and trivial thing introducing jQuery (19KB minified) as a dependency might be a little overkill.
如果你所做的是一个相当小而微不足道的事情,引入jQuery(19KB缩小)作为依赖可能有点矫枉过正。
Don't reinvent the wheel if you don't have to!
如果你不需要,不要重新发明*!
#6
There aren't very many things I find javascript useful for that jQuery doesn't simplify.
我发现javascript不是很多东西,因为jQuery没有简化。
For data validation, I prefer to have just one implementation of that, and since I have to make the server validate everything anyway, Might as well use AJAX for form validation. jQuery is very good at that.
对于数据验证,我更喜欢只有一个实现,因为我必须让服务器验证一切,不妨使用AJAX进行表单验证。 jQuery非常擅长。
For DOM Manipulation, jQuery provides a terse way of expressing what I'm likely to need. Certainly shorter and probably clearer than the 3 to 10 function calls I'd otherwise need.
对于DOM操作,jQuery提供了一种简洁的方式来表达我可能需要的东西。肯定比我需要的3到10个函数调用更短,可能更清晰。
Visual effects are seldom really required. They're hard to make portable too. For the benefit, if I cannot find a prepackaged visual effect, I'm probably better off without it. jQuery UI and several established plugins fit the bill.
视觉效果很少真正需要。他们也很难携带。为了好处,如果我找不到预先打包的视觉效果,没有它我可能会更好。 jQuery UI和几个已建立的插件符合要求。
There are plenty of other JavaScript Libraries, and I'm sure many of them are great, but unless they provide an absolutely indispensable function (eg, Google API), I can probably get jQuery to do it well too, and I'm already familiar with jQuery.
还有很多其他的JavaScript库,我相信它们中的很多都很棒,但除非它们提供了绝对不可或缺的功能(例如Google API),否则我可能会让jQuery做得好,而且我已经熟悉jQuery。
#7
I never use jQuery... as for why? Never really thought about it, I guess I just haven't found a reason to use anything beyond plain Javascript. Plus, I don't like to rely on layers of abstraction I don't understand. (Of course, the things I do with Javascript aren't particularly complicated, so in my case perhaps jQuery really would be more trouble than it's worth.)
我从不使用jQuery ......至于为什么?从来没有真正考虑过它,我想我还没有找到使用普通Javascript之外的任何东西的理由。另外,我不喜欢依赖于我不理解的抽象层。 (当然,我用Javascript做的事情并不是特别复杂,所以在我的情况下,jQuery真的会比它的价值更麻烦。)
#8
Today I added a Javascript patch to a page where I had to move a div out of its original place in the DOM tree and make it a child of the body, so that it could be positioned properly. I knew it would only have to happen once, on startup, to a specific id'd div, and that the page wasn't going to use any other JS behavior. So I wrote it by hand.
今天我在页面中添加了一个Javascript补丁,我必须将div从DOM树中的原始位置移出并使其成为正文的子节点,以便可以正确定位。我知道它只需要在启动时发生一次特定的id'd div,并且该页面不会使用任何其他JS行为。所以我手工写了。
For anything more complex, it's jQuery. The high-level API just saves me so much time that I would otherwise have to spend "talking down" to the computer.
对于任何更复杂的东西,它都是jQuery。高级API只是为我节省了很多时间,否则我不得不花费“低谈”到电脑上。
#9
Well, at least until EcmaScript 5 will come out and until the browsers will implement in their JavaScript Engines a common set of specifications, jQuery is the best tool that we have to solve problems cross browser. Not long ago every time you started a js file you had to start with a browser detect function. I used prototype, mootools, dojo and lately jQuery and I am very satisfied with the help they provide.
好吧,至少在EcmaScript 5问世之前,直到浏览器在他们的JavaScript引擎中实现一套通用规范,jQuery是我们必须通过浏览器解决问题的最佳工具。不久前,每次启动js文件时,都必须从浏览器检测功能开始。我使用了原型,mootools,dojo和最近的jQuery,我对他们提供的帮助非常满意。
#10
Using jQuery just for browser compatibility is not a good enough reason for me to use jQuery. Most javascript framework these days already able to solve that. As for me, I would use jQuery if I ever need a very simple DOM manipulation tool that has Ajax support. If I ever need something beyond that, I would use other javascript framework. I would use Mootools if I need Javascript Object Oriented support and I would use Ext-JS if I ever need extensive built in widgets.
仅仅为了浏览器兼容性而使用jQuery并不足以让我使用jQuery。现在大多数javascript框架已经能够解决这个问题了。至于我,如果我需要一个支持Ajax的非常简单的DOM操作工具,我会使用jQuery。如果我需要其他东西,我会使用其他javascript框架。如果我需要Javascript面向对象的支持,我会使用Mootools,如果我需要大量的内置小部件,我会使用Ext-JS。