通读这篇文章,我想到了函数参数的默认值:
fill = (container, liquid = "coffee") ->
"Filling the #{container} with #{liquid}..."
这很整洁,但后来我尝试了这个:
fill = (container="mug", liquid = "coffee") ->
"Filling the #{container} with #{liquid}..."
alert fill(liquid="juice")
并收到意外警报"Filling the juice with coffee..."
。所以我尝试了这个:
fill = (container="mug", liquid = "coffee") ->
"Filling the #{container} with #{liquid}..."
alert fill(null, "juice")
它奏效了。虽然不漂亮。有没有更好的方法,或者这是惯用的方法?