babel-plugin-transform-private-properties
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "babel-plugin-transform-private-properties", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Provide private variables using WeakMaps", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
# babel-plugin-transform-private-properties | ||
Compile private variables into classes utilizing weak maps. | ||
## Installation | ||
```sh | ||
$ npm install babel-plugin-transform-private-properties | ||
``` | ||
## Usage | ||
### Via `.babelrc` (Recommended) | ||
**.babelrc** | ||
```json | ||
{ | ||
"plugins": ["babel-plugin-transform-private-properties"] | ||
} | ||
``` | ||
### Via CLI | ||
```sh | ||
$ babel --plugins babel-plugin-transform-private-properties script.js | ||
``` | ||
### Via Node API | ||
```javascript | ||
require("babel-core").transform("code", { | ||
plugins: ["babel-plugin-transform-private-properties"] | ||
}); | ||
``` | ||
## Implementation | ||
```javascript | ||
class TestClass { | ||
@Private | ||
p=1; // Decalare private var. Very important, @Private is on it's own line, and is capitalized. | ||
@Private | ||
s; | ||
// Declare non-private class property. | ||
j=2; | ||
constructor() { | ||
this.p = 2; // Setting private. | ||
var self = this // Yes, we follow copying this scope. | ||
, other="gone"; | ||
self.p = this.p.g; // Setting private (p) self is assigned to "this" so assign p to value of p.g | ||
this.func1("constructor",this.p); // Call func1 (which is private) with options. Can't call fun1 outside of this class. | ||
var me; | ||
me = self; // Yes, we follow copying this scope too. | ||
me.s = 5; | ||
me.p = 7; | ||
me.j = 8; | ||
self = me; | ||
self.p = me.p; | ||
} | ||
// You can create public functions with same name as private variable as a way to access the private in a controlled way. | ||
p() { | ||
return this.p; | ||
} | ||
// Getters can be the same as a private variable to gate-keep access. | ||
get s { // this probably shouldn't be allowed to work. | ||
return this.s; | ||
} | ||
@Private | ||
func1(fromWhere) { | ||
console.log("This is the first function called from ",fromWhere); | ||
} | ||
} | ||
``` | ||
This results in the usage such as: | ||
```javascript | ||
var t = new TestClass(); | ||
t.func1("Outside"); // t.func1 is not a function | ||
t.s = "hi" // TypeError: Cannot set property s of #<TestClass> which has only a getter | ||
console.log(t.s); //outputs "5" | ||
console.log(t.p()) // outputs "7" | ||
``` |
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict'; | ||
@@ -6,3 +6,2 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.TestClass = undefined; | ||
@@ -13,6 +12,2 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _j, _func2, _class, _temp; | ||
var _util = require("util"); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
@@ -28,5 +23,7 @@ | ||
var _s = new WeakMap(); | ||
var _func = new WeakMap(); | ||
var TestClass = (_temp = _class = function () { | ||
var TestClass = function () { | ||
function TestClass() { | ||
@@ -43,4 +40,2 @@ _classCallCheck(this, TestClass); | ||
runWith(_p.get(this)); | ||
_p.set(this, 2); | ||
@@ -62,4 +57,6 @@ | ||
var me; | ||
me = this; | ||
me = self; | ||
_s.set(me, 5); | ||
_p.set(me, 7); | ||
@@ -74,3 +71,4 @@ | ||
var otherFunc = function otherFunc() { | ||
var self2 = this; // need to handle this situation three shouldn't == p.get(self2) | ||
// Need to handle changing "self" scope within this function. | ||
var self2 = this; // need to handle this situation three shouldn't == p.get(self2) since "this" is in new scope. | ||
var self3 = self; | ||
@@ -83,3 +81,3 @@ var two = _p.get(self3); | ||
_createClass(TestClass, [{ | ||
key: "p", | ||
key: 'p', | ||
value: function p() { | ||
@@ -89,68 +87,18 @@ return _p.get(this); | ||
}, { | ||
key: "getP", | ||
value: function getP() { | ||
return _p.get(this); | ||
key: 's', | ||
get: function get() { | ||
return _s.get(this); | ||
} | ||
}, { | ||
key: "setP", | ||
value: function setP(v) { | ||
_p.set(this, v); | ||
} | ||
}, { | ||
key: "func2", | ||
value: function func2() { | ||
console.log("This is another function", _p.get(this)); | ||
var z = _p.get(this); | ||
var x = this.j; | ||
} | ||
}]); | ||
return TestClass; | ||
}(), _class.x = (_func2 = new WeakMap(), (_j = new WeakMap(), function () { | ||
function cClass() { | ||
_classCallCheck(this, cClass); | ||
}(); | ||
_func2.set(this, function () { | ||
console.log("This is func2"); | ||
}); | ||
exports.TestClass = TestClass; | ||
console.log("This is a contained class of parent class."); | ||
_j.set(this, parent.p); | ||
} | ||
_createClass(cClass, [{ | ||
key: "func1", | ||
value: function func1() { | ||
console.log("This is func1"); | ||
} | ||
}]); | ||
return cClass; | ||
}())), _temp); | ||
function runWith(value) { | ||
console.log('Run with called with value:', value); | ||
} | ||
var test = new TestClass(); | ||
runWith('outside'); | ||
console.log("Number should be 7"); | ||
test.func2(); | ||
try { | ||
test.func1('outside'); | ||
console.log("accesing private func1 didn't throw error."); | ||
} catch (e) {} | ||
if (test.p) console.log("get private var p shouldn't exist."); | ||
test.p = 10; | ||
if (test.p != 10) console.log("get var p not correct value."); | ||
test.func2(); | ||
exports.default = TestClass; | ||
exports.TestClass = TestClass; | ||
var t = new TestClass(); | ||
// t.func1("Outside"); // t.func1 is not a function | ||
// t.s = "hi" // TypeError: Cannot set property s of #<TestClass> which has only a getter | ||
console.log(t.s); //outputs "5" | ||
console.log(t.p()); // outputs "7" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26894
11
472
101