【发布时间】:2014-05-20 23:35:53
【问题描述】:
我正在查看来自CoffeeScript: Accelerated Development 的以下示例
x = true
showAnswer = (x = x) ->
console.log if x then 'It works!' else 'Nope.'
console.log "showAnswer()", showAnswer()
console.log "showAnswer(true)", showAnswer(true)
console.log "showAnswer(false)", showAnswer(false)
我不明白为什么每次测试都会出现showAnswer(...) undefined。
Nope.
showAnswer() undefined
It works!
showAnswer(true) undefined
Nope.
showAnswer(false) undefined
请解释每个案例的输出。
【问题讨论】:
-
附带说明,如果你想给它一个默认值,你不需要在你的函数之前定义
x = true。(x = true) ->是一个有效的(我认为是首选的)方法签名。
标签: coffeescript