答案是
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
下面是一些測試code:
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]}
""")
我的猜測是:'''f.apply @,arguments''' –
你的猜測是正確的:)。這是一個很好的閱讀JS裝飾器功能(將其翻譯爲CS應該沒有問題):https://github.com/raganwald/homoiconic/blob/master/2013/01/function_and_method_decorators.md – epidemian