jQuery 标准和最佳实践
我目前负责向我们公司内的 Web 开发人员社区推广 jQuery 的使用。 其中一部分涉及介绍课程,但另一部分涉及传达标准和最佳实践。
如果您在 Google 上搜索“jQuery 最佳实践”,您可能会在搜索结果中找到以下内容。 http://www.smashingmagazine.com/2008 /09/16/jquery-examples-and-best-practices/ http://www.artzstudio.com/2009/04/jquery-performance -rules/
这些很有帮助,我已经得到了很多关于它们的有用信息。 然而,我真正感兴趣的是经验丰富的 jQuery 开发人员以及那些可能发现自己与我处于类似位置的人关于最佳实践的任何提示、陷阱、意见等。 任何好的链接也将不胜感激。
编辑:
在我自己的页面上添加了 jQuery 编码标准部分:
I’m currently responsible for rolling out the use of jQuery to the community of Web Developers within our company. Part of this involves presenting a course, however another part involves communicating standards and best practice.
If you Google 'jQuery best practice', you’ll probably find the following among the search results.
http://www.smashingmagazine.com/2008/09/16/jquery-examples-and-best-practices/
http://www.artzstudio.com/2009/04/jquery-performance-rules/
These have been helpful and I have gleamed much useful information on them. However, what I would be really interested in would be any tips, traps, opinions, etc, on best practice from experienced jQuery developers and those who may have found themselves in a similar position to myself. Any good links would also be appreciated.
EDIT:
Added a jQuery Coding Standards section on my own page:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不显眼的 JavaScript(标记和行为分离)
过去,将点击处理程序放在标记内是很常见的。 现在建议您不要在标记中编写 JS 代码,而是通过 DOM 事件包含它。
渐进增强
如果用户使用符合标准的浏览器和/或启用了 JavaScript,他们将获得更好的体验。 即使使用较旧的浏览器或禁用了 JS,网站/Web 应用程序仍然可以访问。
功能检测而不是浏览器检测
抛开以上几点不谈,我真的会专注于传达这样的信息(打破先入为主的观念):JavaScript 是一种玩具语言。 我见过太多有这种想法的开发人员,但从那里开始一切都在走下坡路。 你需要向他们解释 JavaScript 如何是一种非常强大的语言,以及为什么他们需要 JS 库(因为浏览器不一致),尽管 JS 本身非常强大。
祝你好运。
Unobtrusive JavaScript (separation of markup and behavior)
Back in the days, it was common to put your click handler inside the markup. Now it's recommended that you do not write your JS code inside your markup but include it via DOM events.
Progressive enhancement
User gets better experience if they are using standards compliant browser and/or has JavaScript enabled. Website/web application is still accessible even if they have older browser or has JS disabled.
Feature detection and not browser detection
Keeping above points aside, I would really focus on conveying the message (breaking the pre-conceived notion) that JavaScript is a toy language. I have seen too many developers who thinks this way and everything is downhill from there. You need to explain them how JavaScript is a very powerful language and why they need a JS library (because of browser inconsistencies) even though JS itself is very powerful.
Good luck.
您可以在 StackOverflow.com 中找到这个热门主题
要避免的 jQuery 陷阱
非常有趣的有用提示一个接一个地。
以下是我在书签中找到的更多内容:
http://jquery.open2space.com/http://thetoptenme.wordpress.com/2008/08/19/the-complete- guide-for-jquery-developer-reblog/http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspxYou can find this trending topic right here in StackOverflow.com
jQuery pitfalls to avoid
Very interesting useful tips one after the other.
here are some more i found in my bookmarks:
http://jquery.open2space.com/http://thetoptenme.wordpress.com/2008/08/19/the-complete-guide-for-jquery-developer-reblog/http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx我个人开始做的事情是一种 Apps 匈牙利符号 jQuery 集,通过在这些变量前加上
$
前缀,我发现随着我的 jQuery 片段的增长,这变得非常有价值,不仅可以促进将 jQuery 集存储为变量,还可以帮助我跟踪哪些变量实际上是 jQuery 集。
Something I've personally started to do is a sort of an Apps Hungarian Notation for jQuery sets, by prefixing those variables with a
$
I find that as my jQuery snippets grow, this becomes invaluable, not only in the promotion of storing jQuery sets as variables, but to help me keep track of which variables actually are jQuery sets.
jQuery 的工作方式与 JavaScript 的工作方式不同,尽管它们是一样的。 jQuery 适用于 CSS 选择器,例如类名和元素 id。 要在 jQuery 中选择一个项目,您需要执行以下操作:
您将获得页面上符合您条件的所有元素的集合。 然后,您可以对所有这些元素执行操作,而无需任何类型的循环。 记住这一点很重要:
将影响所有附加有
.yourClass
的项目。 提示:如果您要访问$(this)
,您应该将其保存为局部变量:因为它将加速您的函数。
The way that jQuery works is NOT the same way that JavaScript works, even though they are one and the same. jQuery works on CSS selectors, like the class name(s) and the id of elements. To select an item in jQuery, you do:
And you will get a collection of all the elements on the page that fit your criteria. You can then perform your actions on ALL of those elements without any looping of any sort. This is important to remember:
will affect all items with
.yourClass
attached to them. One tip: if you're going to access the$(this)
, you should save it as a local variable:as it will speed up your function.