简体   繁体   English

有没有办法从CoffeeScript类的私有函数访问公共成员

[英]Is there a way to access public members from private function in a CoffeeScript class

How do you convert this CoffeeScript function into a class such that public members can be access by private members ? 您如何将此CoffeeScript函数转换为一个类,以便私有成员可以访问公共成员?

MyClass= -> 
  prVar = 'private'
  prMethod1 = -> 
    console.log 'prMethod'
    self.pubMethod2() 

  public_members =
    pubVar : 'public'
    pubMethod1: -> 
      console.log 'pubMethod1'
      prMethod1() 
    pubMethod2: -> 
      console.log 'pubMethod2'
return public_members 

my = new MyClass()
my.pubMethod1()

class MyClass ?? 类MyClass ??

I hope following code help you : 希望以下代码对您有所帮助:

@MyClass= -> 
  prVar = 'private'
  prMethod1 = -> 
    console.log 'prMethod'
    public_members.pubMethod2() 

  public_members =
    pubVar : 'public'
    pubMethod1: -> 
      console.log 'pubMethod1'
      prMethod1() 
    pubMethod2: ->  
      console.log 'pubMethod2'
  return public_members 

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

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