vlad 6f123ff03e 18.07 | 6 年之前 | |
---|---|---|
.. | ||
lib | 6 年之前 | |
.npmignore | 6 年之前 | |
README.md | 6 年之前 | |
package.json | 6 年之前 |
This plugin transforms es2015 static class properties as well as properties declared with the es2016 property initializer syntax.
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"
npm install --save-dev babel-plugin-transform-class-properties
.babelrc
(Recommended).babelrc
// without options
{
"plugins": ["transform-class-properties"]
}
// with options
{
"plugins": [
["transform-class-properties", { "spec": true }]
]
}
babel --plugins transform-class-properties script.js
require("babel-core").transform("code", {
plugins: ["transform-class-properties"]
});
spec
boolean
, defaults to false
.
Class properties are compiled to use Object.defineProperty
. Static fields are now defined even if they are not initialized.