
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
babel-plugin-transform-private-properties
Advanced tools
Provide private variables using WeakMaps
Compile private variables into classes utilizing weak maps.
$ npm install babel-plugin-transform-private-properties
.babelrc
(Recommended).babelrc
{
"plugins": ["babel-plugin-transform-private-properties"]
}
$ babel --plugins babel-plugin-transform-private-properties script.js
require("babel-core").transform("code", {
plugins: ["babel-plugin-transform-private-properties"]
});
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:
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"
FAQs
Provide private variables using WeakMaps
We found that babel-plugin-transform-private-properties demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.