Introduction
Below is an example I created, demonstrating how to create private
fields and methods within JavaScript ES6 classes.
Example Code
I was able to achieve private
fields and methods by doubling class with class.
class test
{
get(){ console.log('Private Get Hello'); }
constructor()
{
this.fields = [];
const self = this;
class test
{
set counter(_counter) { self.fields['counter'] = _counter; }
get counter() { return self.fields['counter']; }
constructor()
{
console.log(arguments);
}
increase()
{
self.fields['counter'] += 1;
console.log(self.fields);
}
get()
{
self.get();
}
}
return new test(...arguments);
}
}
var a = new test(54321,'abc');
console.log(a);
a.counter = 1234;
a.increase();
a.get();
console.log(a.fields);