Private members in CoffeeScript?

后端 未结 11 2038
太阳男子
太阳男子 2020-12-12 13:37

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 clari

相关标签:
11条回答
  • 2020-12-12 14:05

    If you want only separate private memebers from public, just wrap it in $ variable

    $:
            requirements:
                  {}
            body: null
            definitions: null
    

    and use @$.requirements

    0 讨论(0)
  • 2020-12-12 14:08

    Here's the best article I found about setting public static members, private static members, public and private members, and some other related stuff. It covers much details and js vs. coffee comparison. And for the historical reasons here's the best code example from it:

    # CoffeeScript
    
    class Square
    
        # private static variable
        counter = 0
    
        # private static method
        countInstance = ->
            counter++; return
    
        # public static method
        @instanceCount = ->
            counter
    
        constructor: (side) ->
    
            countInstance()
    
            # side is already a private variable, 
            # we define a private variable `self` to avoid evil `this`
    
            self = this
    
            # private method
            logChange = ->
                console.log "Side is set to #{side}"
    
            # public methods
            self.setSide = (v) ->
                side = v
                logChange()
    
            self.area = ->
                side * side
    
    s1 = new Square(2)
    console.log s1.area()   # output 4
    
    s2 = new Square(3)
    console.log s2.area()   # output 9
    
    s2.setSide 4            # output Side is set to 4
    console.log s2.area()   # output 16
    
    console.log Square.instanceCount() # output 2
    
    0 讨论(0)
  • 2020-12-12 14:10

    Is it even possible without getting "fancy"?

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

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

    Remember, "It's just JavaScript."

    0 讨论(0)
  • 2020-12-12 14:10

    Here is how you can declare private, non-static members in Coffeescript
    For full reference, you can take a look at https://github.com/vhmh2005/jsClass

    class Class
    
      # private members
      # note: '=' is used to define private members
      # naming convention for private members is _camelCase
    
      _privateProperty = 0
    
      _privateMethod = (value) ->        
        _privateProperty = value
        return
    
      # example of _privateProperty set up in class constructor
      constructor: (privateProperty, @publicProperty) ->
        _privateProperty = privateProperty
    
    0 讨论(0)
  • 2020-12-12 14:11

    Since coffee script compiles down to JavaScript the only way you can have private variables is through closures.

    class Animal
      foo = 2 # declare it inside the class so all prototypes share it through closure
      constructor: (value) ->
          foo = value
    
      test: (meters) ->
        alert foo
    
    e = new Animal(5);
    e.test() # 5
    

    This will compile down through the following JavaScript:

    var Animal, e;
    Animal = (function() {
      var foo; // closured by test and the constructor
      foo = 2;
      function Animal(value) {
        foo = value;
      }
      Animal.prototype.test = function(meters) {
        return alert(foo);
      };
      return Animal;
    })();
    
    e = new Animal(5);
    e.test(); // 5
    

    Of course this has all the same limitations as all the other private variables you can have through the use of closures, for example, newly added methods don't have access to them since they were not defined in the same scope.

    0 讨论(0)
提交回复
热议问题