Class properties transform
Transforms class properties
Example
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"
Installation
npm install --save-dev babel-plugin-transform-class-properties
Usage
Via .babelrc
(Recommended)
.babelrc
// without options { "plugins": ["transform-class-properties"] } // with options { "plugins": [ ["transform-class-properties", { "spec": true }] ] }
Via CLI
babel --plugins transform-class-properties script.js
Via Node API
require("babel-core").transform("code", { plugins: ["transform-class-properties"] });
Options
spec
boolean
, defaults to false
.
Class properties are compiled to use Object.defineProperty
. Static fields are now defined even if they are not initialized.
References
© 2018 Sebastian McKenzie
Licensed under the MIT License.
http://babeljs.io/docs/plugins/transform-class-properties/