繁体   English   中英

引用类/实例而不是 object 文字属性的正确方法?

[英]Correct way to refer to class/instance rather than object literal property?

我有一个 object 'foo',其中一个 object 文字作为属性,如下所示。 在该属性中,我想引用 object 'foo' 而不是 object 文字本身。

这只能通过黑客来完成,即通过变量名引用 object 吗? 或者,还有更好的方法?

下面的示例 - 成功时应打印“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')

像这样在构造函数中声明testthing允许@myfunc绑定到“实例”而不是“类”。

您也可以使用'bop': @myfunc而不是'bop': => @myfunc()传递任何 arguments :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM