CoffeeScript 中的私人成员? [英] Private members in CoffeeScript?

查看:18
本文介绍了CoffeeScript 中的私人成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在 CoffeeScript 中创建私有的非静态成员吗​​?目前我正在这样做,它只是使用一个以下划线开头的公共变量来澄清它不应该在类之外使用:

Does somebody know how to make private, non-static members in CoffeeScript? Currently I'm doing this, which just uses a public variable starting with an underscore to clarify that it shouldn't be used outside of the class:

class Thing extends EventEmitter
  constructor: (@_name) ->

  getName: -> @_name

将变量放入类中使其成为静态成员,但如何使其成为非静态成员?有没有可能不被看中"?

Putting the variable in the class makes it a static member, but how can I make it non-static? Is it even possible without getting "fancy"?

推荐答案

有没有可能不被看中"?

Is it even possible without getting "fancy"?

遗憾的是,你必须是 fancy.

Sad to say, you'd have to be fancy.

class Thing extends EventEmitter
  constructor: (name) ->
    @getName = -> name

记住,这只是 JavaScript."

这篇关于CoffeeScript 中的私人成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆