Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

babel-plugin-transform-class-properties

6.24.1 • Public • Published

babel-plugin-transform-class-properties

This plugin transforms es2015 static class properties as well as properties declared with the es2016 property initializer syntax.

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.prototype.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

Keywords

Install

npm i babel-plugin-transform-class-properties

DownloadsWeekly Downloads

1,286,790

Version

6.24.1

License

MIT

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar