Coffeescript “是什么?”&引用;在这个例子中是指咖啡脚本?
我不熟悉咖啡剧本。当我看这份文件的时候 我看到一段代码,比如Coffeescript “是什么?”&引用;在这个例子中是指咖啡脚本?,coffeescript,Coffeescript,我不熟悉咖啡剧本。当我看这份文件的时候 我看到一段代码,比如 atom.commands.add 'atom-text-editor', 'user:insert-date': (event) -> editor = @getModel() editor.insertText(new Date().toLocaleString()) 虽然函数签名看起来 ::add(target, commandName, callback) 那么在代码段中,第二行的:是什么意思?我
atom.commands.add 'atom-text-editor',
'user:insert-date': (event) ->
editor = @getModel()
editor.insertText(new Date().toLocaleString())
虽然函数签名看起来
::add(target, commandName, callback)
那么在代码段中,第二行的
:
是什么意思?我的理解是签名中之前的“user:insert date:
是commandName
。:
后面是“回调”。所以:
是一个参数分隔符,就像,
?我在coffee脚本文档中没有发现冒号只是对象文字的一部分。在CoffeeScript中,当没有歧义时,对象文字周围的大括号是可选的。如果我们添加可选大括号,我们会得到更像JavaScript的东西:
atom.commands.add 'atom-text-editor', {
'user:insert-date': (event) ->
#...
}
因此,正在使用两个参数调用
atom.commands.add
。第一个是字符串'atom-text-editor'
,第二个是一个带有一个键的对象('user:insert date'
),其值是一个匿名函数,只接受一个参数。附加mu的答案太短了(用户完全正确,第二个参数commandName
可以是没有显式大括号{}的对象)
Atom的源代码:
这对我很有帮助。谢谢。
add: (target, commandName, callback) ->
if typeof commandName is 'object'
commands = commandName
disposable = new CompositeDisposable
for commandName, callback of commands
disposable.add @add(target, commandName, callback)
return disposable