【问题标题】:Correct way to return function value instead of binding in Coffeescript在 Coffeescript 中返回函数值而不是绑定的正确方法
【发布时间】:2026-02-22 03:05:01
【问题描述】:

我似乎找不到这个问题的简明答案。在调用 @_instanceMethod 而不是函数绑定本身时,从 _otherInstanceMethod 返回值的正确咖啡脚本方法是什么?

x = _instanceMethod: () ->
    @_otherInstanceMethod key: 'value'

编辑(感谢评论者)

这会返回:

x = function () {
    [...] # function body omitted    
});

代替

x = 'some value returned by _otherInstanceMethod'

我希望返回值而不是绑定到 _otherInstanceMethod 的函数

【问题讨论】:

  • 您能否具体说明问题出在哪里?这个问题在我看来不是很明显。

标签: coffeescript


【解决方案1】:

对 Coffeescript 完全陌生,这是我的错。我正在调用实例方法,例如:

@_instanceMethod

而不是

@_instanceMethod()

不好意思,投票删除

【讨论】:

    【解决方案2】:

    在 CoffeeScript 中,@something 转换为 this.something,无论底层变量类型如何。这意味着您只能将@ 与属性结合使用,而您仍然应该使用旧的this

    【讨论】:

    • 不,@method()@method some_arg 一样,它们将分别被翻译成this.method()this.method(some_arg)