问题描述
在过去的几年中,我几乎一直专注于后端任务,而且我刚刚注意到,大多数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.
我主要在Rails环境中工作,几乎我的所有JavaScript/jQuery都看起来像这样:
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.
无论如何,我只是在浏览其他人的代码,并且发现许多javascripter在我不在的情况下变得越来越漂亮.这并不复杂,但这是我见过的更新/更好的JavaScript方法的典型代表:
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
我一直在寻找有关JavaScript(和/或CoffeeScript)的现代最佳实践/模式的资源,但是运气并不好.简而言之,我应该在什么地方着手提高速度:最佳javascript/coffeescript现代模式&习惯吗?
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?
推荐答案
JavaScript资源
http://coffeescriptcookbook.com/chapters/design_patterns/
这篇关于我的JavaScript模式/做法很臭.我应该在哪里寻求帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!