我看到了这种模式:
Money = (function() {
function Money(rawString) {
this.cents = this.parseCents(rawString);
}
});
Run Code Online (Sandbox Code Playgroud)
在这个CoffeeScript截屏预览中.(截屏视频的主页在这里.)
现在,我不明白这种模式.有一个Money包含Money函数的函数.那是什么意思?
有人能解释一下吗
有没有办法集成CoffeeScript和Eclipse,这样当我在一个窗口中编写CoffeeScript时,另一个会将编译后的代码显示为Javascript?
我会等待答案.谢谢.
背景:我正在尝试将使用带有D3.js数据可视化库的Crossfilter库的一些JavaScript代码转换为CoffeeScript.
将JavaScript forEach循环/函数转换为CoffeeScript的最佳方法是什么?
这是JavaScript代码:
// A little coercion, since the CSV is untyped.
flights.forEach(function(d, i) {
d.index = i;
d.date = parseDate(d.date);
d.delay = +d.delay;
d.distance = +d.distance;
});
Run Code Online (Sandbox Code Playgroud)
CoffeeScript可以在循环中执行内联函数吗?现在我猜我需要把它分解成一个函数和循环:
coerce = (d) ->
d.index = 1
d.date = parseDate(d.date)
d.delay = +d.delay
d.distance = +d.distance
coerce(flights) for d in flights
Run Code Online (Sandbox Code Playgroud) 如果说我有一个数组,我会迭代数组,但做一些与第一个和最后一个元素不同的东西.我该怎么办?
以下面的代码为例,如何提醒元素a和e?
array = [a,b,c,d,e]
for element in array
console.log(element)
Run Code Online (Sandbox Code Playgroud)
谢谢.
为什么我们{EventEmitter} = require 'events'在扩展Node.js类时将变量包装在花括号中?
例如,Trevor Burnham在他的Event-Driven CoffeeScript 教程中以这种方式扩展了Node的EventEmitter:
{EventEmitter} = require 'events'
class Rooster extends EventEmitter
constructor: ->
@on 'wake', -> console.log 'COCKADOODLEDOO!'
(foghorn = new Rooster).emit 'wake' # COCKADOODLEDOO!
Run Code Online (Sandbox Code Playgroud) .:编辑:. 如果您遇到同样的问题,请检查您的变量初始化,我忘记了新的.
我一直得到TypeError:this._ensureElement不是一个函数,并且得到了TypeError:this._reset早先不是一个函数,但我无法真正重新创建后者的确切设置.
我的脚本顺序正确:
<script src="js/components/jquery.js"></script>
<script src="js/components/underscore.js"></script>
<script src="js/components/backbone.js"></script>
<script src="js/script.js"></script>
Run Code Online (Sandbox Code Playgroud)
我的模型在收藏前已经注册.
这是一个购物车应用程序.
#namespacing
App =
Collection : {}
Model : {}
View : {}
###
MODEL
###
class ModelItem extends Backbone.Model
#default values
defaults:
name : 'Product Name'
quantity : 0
unit : 'kg'
#Increase or decrease the quantity
change_quantity : (type) ->
qty = @get 'quantity'
@set 'quantity', if type is 'increase' then ++qty else --qty
###
COLLECTION
###
class CollectionItems …Run Code Online (Sandbox Code Playgroud) 我正在使用Apple的CoreDataBooks示例项目与Zucchini自动化测试框架一起使用.我需要在coffeescript中使用的视图名称来管理它,如下所示:
class BooksScreen extends Screen
anchor: -> $("navigationBar[name=Authors]")
在此示例中,作者用于标识视图.我想在xcode中我们在每个场景的视图控制器类之后都有这个名字,比如New Book场景:

问题是,我不知道如何设置该名称,所以我可以在coffeescript中管理它.这该怎么做?
我是CoffeeScript的新手,但我真的很期待将JavaScript代码编写成类似于Ruby的东西.
为了使用CoffeeScript启动和运行教程或截屏,可以推荐2-3个好的资源?
如何在Coffeescript中打破/继续嵌套循环?我有类似的东西:
for cat in categories
for job in jobs
if condition
do(this)
## Iterate to the next cat in the first loop
Run Code Online (Sandbox Code Playgroud)
另外,有没有办法将整个第二个循环作为条件包装到第一个循环中的另一个函数?例如
for cat in categories
if conditionTerm == job for job in jobs
do(this)
## Iterate to the next cat in the first loop
do(that) ## Execute upon eliminating all possibilities in the second for loop,
## but don't if the 'if conditionTerm' was met
Run Code Online (Sandbox Code Playgroud) 我想使用CoffeeScript存在运算符来检查undefined的一些对象属性.但是,我遇到了一个小问题.
像这样的代码:
console.log test if test?
Run Code Online (Sandbox Code Playgroud)
编译为:
if (typeof test !== "undefined" && test !== null) console.log(test);
Run Code Online (Sandbox Code Playgroud)
这是我希望看到的行为.但是,当我尝试将它用于对象属性时,如下所示:
console.log test.test if test.test?
Run Code Online (Sandbox Code Playgroud)
我得到类似的东西:
if (test.test != null) console.log(test.test);
Run Code Online (Sandbox Code Playgroud)
哪个看起来不像是对未定义的检查.我可以实现与使用对象相同(1:1)行为的唯一方法是使用更大的检查:
console.log test.test if typeof test.test != "undefined" and test.test != null
Run Code Online (Sandbox Code Playgroud)
问题是 - 我做错了吗?或者编译后的代码是否足以检查属性是否存在(带有类型转换的空检查)?
coffeescript ×10
javascript ×5
arrays ×1
backbone.js ×1
crossfilter ×1
d3.js ×1
eclipse ×1
foreach ×1
ios ×1
node.js ×1
xcode ×1
zucchini ×1