How do JavaScript ES5 and ES6 make class privacy?



  • Not the protects that are accepted to be referred to as this._play(), i.e. private, so that they are not accessible only from the outside, as well as from the classes of heirs, so that there is no conflict of names.

    It is desirable, however, that Symbol is not maintained everywhere.

    I see it's in ES6, but in ES5 how to be with it?



  • as an alternative

    function MyClass() {
      var self = this;
      self.one = 1; // private;
      self.two = 2;
      return {
        add: function(val) {
          return self.one + val;
        },
        two: self.two
      }
    }
    

    var mc = new MyClass();
    console.log(mc.add(mc.two));




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2