【发布时间】:2015-01-05 03:58:27
【问题描述】:
我已经设置了一个带有可变参数的函数:
myfunc: (cmd, args...)->
# cmd is a string
# args is an array
并且可以这样调用:
myfunc("one") # cmd = "one", args = undefined
myfunc("one","two") # cmd = "one", args = ["two"]
# etc...
现在,如果我想用未知数量的参数调用它怎么办?
假设我想传递一个 args 数组而不是 arg1, arg2, arg3,.. 这怎么可能?
尝试myfunc("one",["two","three"]) 或myfunc("one",someArgs) 会导致不幸:
# cmd = "one"
# args = [ ["two","three"] ];
想法?
P.S.我通过在我的函数中添加这些超简单的行来实现它。但是就没有别的办法了吗?
if args? and args[0] instanceof Array
args = args[0]
【问题讨论】:
标签: javascript variables coffeescript argument-passing