在过去的几年里,我几乎只专注于后端任务,我刚刚注意到,在我不在的情况下,大多数 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.
无论如何,我只是浏览了一些其他人的代码,并注意到许多 javascripters 在我不在的情况下变得更漂亮了.这并不复杂,但它是我所见过的更新/更好的 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 应用程序架构的模式
面向初学者的基本 JavaScript 设计模式,第 1 卷.
JavaScript 模式
jQuery 应用程序架构工具
http://coffeescriptcookbook.com/chapters/design_patterns/
这篇关于我的 JavaScript 模式/实践很糟糕.我应该去哪里寻求帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!