Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@babel/plugin-proposal-class-properties
Advanced tools
This plugin transforms static class properties as well as properties declared with the property initializer syntax
The @babel/plugin-proposal-class-properties package is a Babel plugin that allows you to use the class properties syntax in JavaScript. This syntax is part of a proposal for ECMAScript and has not been finalized yet, but this plugin lets you use it with Babel to compile your code to a version of JavaScript that is compatible with current environments. It supports both static and instance properties, as well as public and private fields.
Static Class Properties
Allows you to define static properties on a class, which are properties that are accessed on the class itself, not on instances of the class.
class MyClass {\n static myStaticProp = 42;\n}
Instance Class Properties
Enables you to define properties directly on class instances, with an initial value, without needing to set them inside the class constructor.
class MyClass {\n myInstanceProp = 'default value';\n}
Public Class Fields
Supports the declaration of public fields in a class, which are accessible from outside of the class.
class MyClass {\n publicField = 'some value';\n}
Private Class Fields
Allows the definition of private fields in a class, which are only accessible within the class itself using the '#' syntax.
class MyClass {\n #privateField = 'hidden value';\n}
This package is similar to @babel/plugin-proposal-class-properties but focuses on adding support for private methods and accessors in classes. It allows you to define methods that can only be called from within the class itself.
This package provides functionality for decorators, which are a stage 2 proposal for JavaScript. Decorators are a way to modify classes or properties at design time. While not directly related to class properties, decorators often work in conjunction with them to add annotations or modify class behavior.
This package transforms ES2015 classes to ES5, which is a broader transformation that includes but is not limited to class properties. It's more comprehensive in terms of class-related transformations but does not specifically target the class properties proposal.
This plugin transforms class properties
Below is a class with four class properties which will be transformed.
class Bork {
//Property initializer syntax
instanceProperty = "bork";
boundFunction = () => {
return this.instanceProperty;
};
//Static class properties
static staticProperty = "babelIsCool";
static staticFunction = function() {
return Bork.staticProperty;
};
}
let myBork = new Bork;
//Property initializers are not on the prototype.
console.log(myBork.__proto__.boundFunction); // > undefined
//Bound functions are bound to the class instance.
console.log(myBork.boundFunction.call(undefined)); // > "bork"
//Static function exists on the class.
console.log(Bork.staticFunction()); // > "babelIsCool"
npm install --save-dev @babel/plugin-proposal-class-properties
.babelrc
(Recommended).babelrc
Without options:
{
"plugins": ["@babel/plugin-proposal-class-properties"]
}
With options:
{
"plugins": [
["@babel/plugin-proposal-class-properties", { "loose": true }]
]
}
babel --plugins @babel/plugin-proposal-class-properties script.js
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-proposal-class-properties"]
});
loose
boolean
, defaults to false
.
When true
, class properties are compiled to use an assignment expression instead of Object.defineProperty
.
class Bork {
static a = 'foo';
static b;
x = 'bar';
y;
}
Without { "loose": true }
, the above code will compile to the following, using Object.defineProperty
:
var Bork = function Bork() {
babelHelpers.classCallCheck(this, Bork);
Object.defineProperty(this, "x", {
configurable: true,
enumerable: true,
writable: true,
value: 'bar'
});
Object.defineProperty(this, "y", {
configurable: true,
enumerable: true,
writable: true,
value: void 0
});
};
Object.defineProperty(Bork, "a", {
configurable: true,
enumerable: true,
writable: true,
value: 'foo'
});
Object.defineProperty(Bork, "b", {
configurable: true,
enumerable: true,
writable: true,
value: void 0
});
However, with { "loose": true }
, it will compile using assignment expressions:
var Bork = function Bork() {
babelHelpers.classCallCheck(this, Bork);
this.x = 'bar';
this.y = void 0;
};
Bork.a = 'foo';
Bork.b = void 0;
FAQs
This plugin transforms static class properties as well as properties declared with the property initializer syntax
The npm package @babel/plugin-proposal-class-properties receives a total of 14,314,848 weekly downloads. As such, @babel/plugin-proposal-class-properties popularity was classified as popular.
We found that @babel/plugin-proposal-class-properties demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.