babel-plugin-transform-private-properties
Compile private variables into classes utilizing weak maps.
Installation
$ npm install babel-plugin-transform-private-properties
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["babel-plugin-transform-private-properties"]
}
Via CLI
$ babel --plugins babel-plugin-transform-private-properties script.js
Via Node API
require("babel-core").transform("code", {
plugins: ["babel-plugin-transform-private-properties"]
});
Implementation
class TestClass {
@Private
p=1;
@Private
s;
j=2;
constructor() {
this.p = 2;
var self = this
, other="gone";
self.p = this.p.g;
this.func1("constructor",this.p);
var me;
me = self;
me.s = 5;
me.p = 7;
me.j = 8;
self = me;
self.p = me.p;
}
p() {
return this.p;
}
get s {
return this.s;
}
@Private
func1(fromWhere) {
console.log("This is the first function called from ",fromWhere);
}
}
This results in the usage such as:
var t = new TestClass();
t.func1("Outside");
t.s = "hi"
console.log(t.s);
console.log(t.p())