[英]Correct way to refer to class/instance rather than object literal property?
I have an object 'foo', with an object literal as a property, as shown below.我有一个 object 'foo',其中一个 object 文字作为属性,如下所示。 Inside that property, I'd like to refer to the object 'foo' rather than the object literal itself.
在该属性中,我想引用 object 'foo' 而不是 object 文字本身。
Can this only be done with hacks, ie, referring to the object by its variable name?这只能通过黑客来完成,即通过变量名引用 object 吗? Or is there a better way?
或者,还有更好的方法?
Example below - should print 'woo' on success.下面的示例 - 成功时应打印“woo”。
class Foo
myfunc: =>
console.log('woo')
testthing: {
'foo':'bar'
'baz':'boo'
'bop': =>
@myfunc()
}
window.foo = new Foo
foo.testthing.bop()
class Foo
constructor: ->
@testthing =
'foo':'bar'
'baz':'boo'
'bop': => @myfunc()
myfunc: =>
console.log('woo')
Declaring testthing
in the constructor like this allows @myfunc
to be bound to the 'instance' rather than the 'class'.像这样在构造函数中声明
testthing
允许@myfunc
绑定到“实例”而不是“类”。
You could also use 'bop': @myfunc
instead of 'bop': => @myfunc()
to pass along any arguments:)您也可以使用
'bop': @myfunc
而不是'bop': => @myfunc()
传递任何 arguments :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.