答案是
f.apply @, arguments
因此包装看起来像
createWrapper= (f) ->
->
# do the logging or whatever I want before I call the original
result= f.apply @, arguments
# do whatever after the original function call
result
或者如果您不想在通话后做任何事情
createWrapper= (f) ->
->
# do the logging or whatever I want before I call the original
f.apply @, arguments
或者,如果您想更明确一点,可以使用splat。这样做的好处是参数是一个列表,您可以对参数应用列表操作。
createWrapper= (f) ->
(args...) ->
# do the logging or whatever I want before I call the original
result= f.apply @, args
# do whatever after the original function call
result
这是一些测试代码:
createWrapper= (f) ->
(args...) ->
# do the logging or whatever I want before I call the original
result= f.apply @, args
# do whatever after the original function call
result
class cl
i: 10
f: (x) ->
x + @i
getf: (x) ->
(x)=>@f(x)
c =new cl
f1 = c.getf()
g1 = createWrapper(f1)
if f1(1)!=g1(1)
throw new Error("failed f1 #{f1 1} != #{g1 1}")
f2 = (x) -> x+20
g2 = createWrapper(f2)
if f2(1)!=g2(1)
throw new Error("failed f2 #{f2 1} != #{g2 1}")
f3 = (args...) -> "#{args}"
g3 = createWrapper(f3)
if f3(1,2,3)!=g3(1,2,3)
throw new Error("failed f3 #{f3 1,2,3} != #{g3 1,2,3}")
f4 = -> arguments[0]
g4 = createWrapper(f4)
if f4(1)!=g4(1)
throw new Error("failed f4 #{f4 1} != #{g4 1}")
f5 = c.f
g5 = createWrapper(f5)
if f5.apply(c,[1])!=g5.apply(c,[1])
throw new Error("failed f5 #{f5.apply c,[1]} 1= #{g5.apply c,[1]}")
alert("""
Everything is OK:
f1 #{f1 1} == #{g1 1}
f2 #{f2 1} == #{g2 1}
f3 #{f3 1,2,3} == #{g3 1,2,3}
f4 #{f4 1} == #{g4 1}
f5 #{f5.apply c,[1]} == #{g5.apply c,[1]}
""")