在过去的几年里,我几乎专门从事后端任务,我刚刚注意到,在我不在的情况下,大多数 JavaScript(和 CoffeeScript)项目都变得非常漂亮。
我主要在 Rails 环境中工作,几乎所有的 JavaScript/jQuery 过去都是这样的:
$(an_element).an_event(function() {
stuff_i_want_to_do;
})
$(another_element).some_other_event(function() {
some_other_stuff_i_want_to_do;
})
除了回调之外,基本上就是这样。
不管怎样,我只是浏览了一些其他人的代码,发现在我不在的情况下,许多 JavaScript 开发人员变得更加漂亮了。这并不复杂,但它是我所见过的更新/更好的 JavaScript 方法的典型:
jQuery ->
if $('#products').length
new ProductsPager()
class ProductsPager
constructor: (@page = 1) ->
$(window).scroll(@check)
check: =>
if @nearBottom()
@page++
$(window).unbind('scroll', @check)
$.getJSON($('#products').data('json-url'), page: @page, @render)
#
nearBottom: =>
$(window).scrollTop() > $(document).height() - $(window).height() - 50
render: (products) =>
for product in products
$('#products').append Mustache.to_html($('#product_template').html(), product)
$(window).scroll(@check) if products.length > 0
我一直在寻找有关 JavaScript(和/或 CoffeeScript)的现代最佳实践/模式的资源,但我还没有运气很好。简而言之,我应该在哪里了解最新的知识:最好的 javascript/coffeescript 现代模式 &做法?
I've been working almost exclusively on back-end tasks for the past few years, and I've just noticed that most JavaScript (and CoffeeScript) projects have got a helluva lot prettier in my absence.
I work primarily in a rails environment, and almost all my JavaScript/jQuery used to look like this:
$(an_element).an_event(function() {
stuff_i_want_to_do;
})
$(another_element).some_other_event(function() {
some_other_stuff_i_want_to_do;
})
Callbacks aside, that's pretty much been it.
Anyhow, was just browsing through some other folks' code and noticed many javascripters have been getting a lot prettier in my absence. This isn't complex, but it's typical of the newer/better approach to JavaScript I've been seeing:
jQuery ->
if $('#products').length
new ProductsPager()
class ProductsPager
constructor: (@page = 1) ->
$(window).scroll(@check)
check: =>
if @nearBottom()
@page++
$(window).unbind('scroll', @check)
$.getJSON($('#products').data('json-url'), page: @page, @render)
#
nearBottom: =>
$(window).scrollTop() > $(document).height() - $(window).height() - 50
render: (products) =>
for product in products
$('#products').append Mustache.to_html($('#product_template').html(), product)
$(window).scroll(@check) if products.length > 0
I've been looking for resources on modern best practices/patterns for JavaScript (and/or CoffeeScript), but I haven't had much luck. So in short, where should I look to be brought up to speed re: best javascript/coffeescript modern patterns & practices?
发布评论
评论(7)
JavaScript 资源
大规模 JavaScript 应用程序架构模式
初学者必备 JavaScript 设计模式,第 1 卷。
JavaScript 模式
特定于 jQuery 的
jQuery 应用程序架构工具
CoffeeScript
http://coffeescriptcookbook.com/chapters/design_patterns/
JavaScript resources
Patterns For Large-Scale JavaScript Application Architecture
Essential JavaScript Design Patterns For Beginners, Volume 1.
JavaScript Patterns
jQuery specific
Tools For jQuery Application Architecture
CoffeeScript
http://coffeescriptcookbook.com/chapters/design_patterns/
以下是我收集的一些链接:
常规
http://eloquentjavascript.net/
http://jqfundamentals.com/
模式
http://addyosmani.com/resources/essentialjsdesignpatterns/book/
继承
http://blog.vjeux.com/2011/javascript/how-prototypal-inheritance-really-works.html
模块模式
http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
http://ajaxian.com/archives/a-javascript-module-pattern
http://edspencer.net/ 2009/10/javascript-module-pattern-overused-dangerous-and-bloody-annoying.html
新关键字
JavaScript 的“new”关键字是否被认为有害? http://ejohn.org/blog/simple-class-instantiation
闭包
http://www.bennadel.com/blog/1482-A-Graphical-Explanation-Of-Javascript-Closures-In-A-jQuery-Context.htm
JavaScript 闭包如何工作?
http://skilldrick.co.uk/2011/04/closures -explained-with-javascript/
通过引用/值传递
http://snook.ca/archives/javascript/javascript_pass
教授 JavaScript
http://concisionandconcinnity.blogspot.com/2010/03/notes-on-teaching-javascript.html
此关键字
http://alebelcor.blogspot.com/2011/07/this-keyword-in-javascript.html
http://yehudakatz.com/2011/08 /11/understanding-javascript-function-inspiration-and-this/
对象文字
http://blog.rebeccamurphey.com/2009/ 10/15/使用对象组织您的代码
原型
http://yehudakatz.com/2011/08/12/understanding-prototypes -in-javascript/
Here are some links I've collected:
General
http://eloquentjavascript.net/
http://jqfundamentals.com/
Patterns
http://addyosmani.com/resources/essentialjsdesignpatterns/book/
Inheritance
http://blog.vjeux.com/2011/javascript/how-prototypal-inheritance-really-works.html
Module pattern
http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
http://ajaxian.com/archives/a-javascript-module-pattern
http://edspencer.net/2009/10/javascript-module-pattern-overused-dangerous-and-bloody-annoying.html
New keyword
Is JavaScript's "new" keyword considered harmful? http://ejohn.org/blog/simple-class-instantiation
Closures
http://www.bennadel.com/blog/1482-A-Graphical-Explanation-Of-Javascript-Closures-In-A-jQuery-Context.htm
How do JavaScript closures work?
http://skilldrick.co.uk/2011/04/closures-explained-with-javascript/
Pass by reference / value
http://snook.ca/archives/javascript/javascript_pass
Teaching JavaScript
http://concisionandconcinnity.blogspot.com/2010/03/notes-on-teaching-javascript.html
This keyword
http://alebelcor.blogspot.com/2011/07/this-keyword-in-javascript.html
http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/
Object Literal
http://blog.rebeccamurphey.com/2009/10/15/using-objects-to-organize-your-code
Prototype
http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/
我喜欢CoffeeScript Cookbook。它解释了很多并且包含很多例子。
你可能喜欢第 12 章“设计模式”
I like the CoffeeScript Cookbook. It explains alot and contains many examples.
You probably like the 12th chapter called Design patterns
你需要一本像“JavaScript Patterns”这样的好书,以及一个同样好的想法/环境如“Fiddle”用于练习。
You need a good book like "JavaScript Patterns" accompanied by an equally good ide/environment like "Fiddle" for practicing.
我认为阅读常见模式不会帮助您编写真正好的代码。还算不错的代码,但不是真正的好代码。我会登录 irc.freenode.net 并在 ##javascript 和 #coffeescript 中寻求帮助 - 至少在 #coffeescript 中,有很多人会帮助您改进放入 要点。
I don't think that reading on common patterns will help you write really good code. Moderately good code, but not really good code. I'd login to irc.freenode.net and ask in ##javascript and #coffeescript for help - at least in #coffeescript, there are many people who will help you improve code you put in a gist.
我没有看到你的旧代码有问题。或者使用较新的代码。基本上,只需遵循与 Ruby 相同的原则:无情地重构,并让重构中出现好的架构。
I don't see the problem with your older code. Or with the newer code. Basically, just follow the same principles you'd follow with Ruby: refactor mercilessly and let good architecture emerge from the refactoring.
如果您需要使用大规模 javascript 参考架构的完整实现,请查看:
http://boilerplatejs.org
它是一些模式的集合以及一些优秀库与现成示例应用程序的集成。我写这篇文章是为了分享我在几个大型 JS 项目工作后的经验。
If you need to play with a complete implementation of a large scale javascript reference architecture, have a look at:
http://boilerplatejs.org
It is a collection of patterns and integration of some good libraries with readymade sample application to start with. I wrote it to share my experience after working on couple of large JS projects.