babel-plugin-transform-class-constructor-call (deprecated)
Proposal Withdrawn: can be solved with decorators.
This plugin allows Babel to transform class constructors.
It basically allows to use the new.target feature on ES2015 classes:
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
call constructor(x, y) {
return new Point(x, y);
}
}
let p1 = new Point(1, 2);
let p2 = Point(3, 4);
Example
Date example
The javascript Date works this way:
let now = new Date();
console.log(now.getMonth());
console.log(now.toString());
let nowStr = Date();
console.log(nowStr);
It is currently possible to implement something like that using new.target (see example in proposal) and this new feature makes it available for ES2015 classes.
A date implementation could be:
class Date {
constructor() {
}
call constructor() {
let date = new Date();
return date.toString();
}
}
let now = new Date();
let nowStr = Date();
Installation
npm install --save-dev babel-plugin-transform-class-constructor-call
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["transform-class-constructor-call"]
}
Via CLI
babel --plugins transform-class-constructor-call script.js
Via Node API
require("babel-core").transform("code", {
plugins: ["transform-class-constructor-call"]
});
References