首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >CoffeeScript中的私有成员?

CoffeeScript中的私有成员?
EN

Stack Overflow用户
提问于 2011-01-14 05:43:24
回答 7查看 29.4K关注 0票数 84

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

代码语言:javascript
复制
class Thing extends EventEmitter
  constructor: (@_name) ->

  getName: -> @_name

将变量放入类中使其成为静态成员,但如何使其成为非静态成员呢?有没有可能在不“花哨”的情况下?

EN

回答 7

Stack Overflow用户

发布于 2011-12-01 07:12:24

类只是函数,所以它们创建作用域。在这个作用域中定义的所有内容从外部都是不可见的。

代码语言:javascript
复制
class Foo
  # this will be our private method. it is invisible
  # outside of the current scope
  foo = -> "foo"

  # this will be our public method.
  # note that it is defined with ':' and not '='
  # '=' creates a *local* variable
  # : adds a property to the class prototype
  bar: -> foo()

c = new Foo

# this will return "foo"
c.bar()

# this will crash
c.foo

coffeescript将其编译为以下代码:

代码语言:javascript
复制
(function() {
  var Foo, c;

  Foo = (function() {
    var foo;

    function Foo() {}

    foo = function() {
      return "foo";
    };

    Foo.prototype.bar = function() {
      return foo();
    };

    return Foo;

  })();

  c = new Foo;

  c.bar();

  c.foo();

}).call(this);
票数 203
EN

Stack Overflow用户

发布于 2012-09-26 13:54:02

我想展示一些更花哨的东西

代码语言:javascript
复制
class Thing extends EventEmitter
  constructor: ( nm) ->
    _name = nm
    Object.defineProperty @, 'name',
      get: ->
        _name
      set: (val) ->
        _name = val
      enumerable: true
      configurable: true

现在你可以做

代码语言:javascript
复制
t = new Thing( 'Dropin')
#  members can be accessed like properties with the protection from getter/setter functions!
t.name = 'Dragout'  
console.log t.name
# no way to access the private member
console.log t._name
票数 11
EN

Stack Overflow用户

发布于 2014-11-23 21:35:31

这里有一个解决方案,它借鉴了这里的几个其他答案以及https://stackoverflow.com/a/7579956/1484513。它将私有实例(非静态)变量存储在私有类(静态)数组中,并使用对象ID来知道该数组的哪个元素包含属于每个实例的数据。

代码语言:javascript
复制
# Add IDs to classes.
(->
  i = 1
  Object.defineProperty Object.prototype, "__id", { writable:true }
  Object.defineProperty Object.prototype, "_id", { get: -> @__id ?= i++ }
)()

class MyClass
  # Private attribute storage.
  __ = []

  # Private class (static) variables.
  _a = null
  _b = null

  # Public instance attributes.
  c: null

  # Private functions.
  _getA = -> a

  # Public methods.
  getB: -> _b
  getD: -> __[@._id].d

  constructor: (a,b,@c,d) ->
    _a = a
    _b = b

    # Private instance attributes.
    __[@._id] = {d:d}

# Test

test1 = new MyClass 's', 't', 'u', 'v'
console.log 'test1', test1.getB(), test1.c, test1.getD()  # test1 t u v

test2 = new MyClass 'W', 'X', 'Y', 'Z'
console.log 'test2', test2.getB(), test2.c, test2.getD()  # test2 X Y Z

console.log 'test1', test1.getB(), test1.c, test1.getD()  # test1 X u v

console.log test1.a         # undefined
console.log test1._a        # undefined

# Test sub-classes.

class AnotherClass extends MyClass

test1 = new AnotherClass 's', 't', 'u', 'v'
console.log 'test1', test1.getB(), test1.c, test1.getD()  # test1 t u v

test2 = new AnotherClass 'W', 'X', 'Y', 'Z'
console.log 'test2', test2.getB(), test2.c, test2.getD()  # test2 X Y Z

console.log 'test1', test1.getB(), test1.c, test1.getD()  # test1 X u v

console.log test1.a         # undefined
console.log test1._a        # undefined
console.log test1.getA()    # fatal error
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4685626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档