[javascript] Private properties in JavaScript ES6 classes

I believe it is possible to get 'best of both worlds' using closures inside constructors. There are two variations:

All data members are private

_x000D_
_x000D_
function myFunc() {_x000D_
   console.log('Value of x: ' + this.x);_x000D_
   this.myPrivateFunc();_x000D_
}_x000D_
_x000D_
function myPrivateFunc() {_x000D_
   console.log('Enhanced value of x: ' + (this.x + 1));_x000D_
}_x000D_
_x000D_
class Test {_x000D_
   constructor() {_x000D_
_x000D_
      let internal = {_x000D_
         x : 2,_x000D_
      };_x000D_
      _x000D_
      internal.myPrivateFunc = myPrivateFunc.bind(internal);_x000D_
      _x000D_
      this.myFunc = myFunc.bind(internal);_x000D_
   }_x000D_
};
_x000D_
_x000D_
_x000D_

Some members are private

NOTE: This is admittedly ugly. If you know a better solution, please edit this response.

_x000D_
_x000D_
function myFunc(priv, pub) {_x000D_
   pub.y = 3; // The Test object now gets a member 'y' with value 3._x000D_
   console.log('Value of x: ' + priv.x);_x000D_
   this.myPrivateFunc();_x000D_
}_x000D_
_x000D_
function myPrivateFunc() {_x000D_
   pub.z = 5; // The Test object now gets a member 'z' with value 3._x000D_
   console.log('Enhanced value of x: ' + (priv.x + 1));_x000D_
}_x000D_
_x000D_
class Test {_x000D_
   constructor() {_x000D_
      _x000D_
      let self = this;_x000D_
_x000D_
      let internal = {_x000D_
         x : 2,_x000D_
      };_x000D_
      _x000D_
      internal.myPrivateFunc = myPrivateFunc.bind(null, internal, self);_x000D_
      _x000D_
      this.myFunc = myFunc.bind(null, internal, self);_x000D_
   }_x000D_
};
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to class

String method cannot be found in a main class method Class constructor type in typescript? ReactJS - Call One Component Method From Another Component How do I declare a model class in my Angular 2 component using TypeScript? When to use Interface and Model in TypeScript / Angular Swift Error: Editor placeholder in source file Declaring static constants in ES6 classes? Creating a static class with no instances In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric Static vs class functions/variables in Swift classes?

Examples related to ecmascript-6

"Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6 where is create-react-app webpack config and files? Can (a== 1 && a ==2 && a==3) ever evaluate to true? How do I fix "Expected to return a value at the end of arrow function" warning? Enums in Javascript with ES6 Home does not contain an export named Home How to scroll to an element? How to update nested state properties in React eslint: error Parsing error: The keyword 'const' is reserved Node.js ES6 classes with require

Examples related to private-members

Private properties in JavaScript ES6 classes Private Variables and Methods in Python Accessing private member variables from prototype-defined functions

Examples related to es2015

Private properties in JavaScript ES6 classes