【发布时间】:2012-12-16 10:51:30
【问题描述】:
我有以下代码
class Foo
a: ->
console.log arguments.callee.name
b: ->
@a()
c: ->
@a()
f = new Foo
f.b() #=> should output 'b'
f.c() #=> should output 'c'
问题:如何在我的类中获取调用函数的名称?
这是一个用例
class Something extends Stream
foo: ->
_helper 'foo', 'a', 'b', 'c'
bar: ->
_helper 'bar', 'my neighbor totoro'
dim: ->
_helper 'dim', 1, 2, 3
sum: ->
_helper 'sum', 'hello', 'world'
_helper: (command, params...) ->
@emit 'data', command, params...
something = new Something
something.foo()
something.bar()
# ...
我不想重复将每次调用的方法名称发送到我的私有 _helper 方法
【问题讨论】:
-
你有充分的理由吗?
arguments.callee通常应该避免使用,不能在严格的代码中使用,我相信 CoffeeScript 默认使用。 -
@minitech、
b和c将是可公开访问的包装方法,由a作为参数传递给我的另一段代码。这将有助于防止大量重复。 -
@naomik - 你能给我们提供
a、b和c的代码吗?您无法访问arguments.callee,但我们或许可以提出解决方法。 -
这会毁了你的表现,仅供参考。
arguments将所有现代 VM 踢入解释模式,而不是 JIT 编译为机器代码。 -
@Domenic,所以你建议只实现我在用例中的内容(第二个代码粘贴)?
标签: javascript node.js coffeescript