babel-plugin-transform-class-constructor-call
This plugin allows Babel to transform class constructors (deprecated)
Last updated 8 years ago by hzoo .
MIT · Repository · Original npm · Tarball · package.json
$ npm install babel-plugin-transform-class-constructor-call 
SYNC missed versions from official npm registry.

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); // OK
let p2 = Point(3, 4); // OK

Example

Date example

The javascript Date works this way:

// You can get a Date instance using the new keyword
let now = new Date();
console.log(now.getMonth()); // Prints '3'
console.log(now.toString()); // Prints 'Mon Apr 11 2016 13:26:07 GMT+0100 (BST)'

// You can get a string of the current date using Date as a function:
let nowStr = Date();
console.log(nowStr); // Prints 'Mon Apr 11 2016 13:26:07 GMT+0100 (BST)'

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(); // Get a Date instance
let nowStr = Date(); // Use the 'call constructor()' part to get a string value of the current 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

Current Tags

  • 6.24.1                                ...           latest (8 years ago)

21 Versions

  • 6.24.1                                ...           8 years ago
  • 6.22.0                                ...           8 years ago
  • 6.18.0                                ...           8 years ago
  • 6.8.0                                ...           9 years ago
  • 6.6.5                                ...           9 years ago
  • 6.6.4                                ...           9 years ago
  • 6.5.0-1                                ...           9 years ago
  • 6.5.0                                ...           9 years ago
  • 6.4.0                                ...           9 years ago
  • 6.3.13                                ...           9 years ago
  • 6.2.4                                ...           9 years ago
  • 6.1.18                                ...           9 years ago
  • 6.1.17                                ...           9 years ago
  • 6.1.13                                ...           9 years ago
  • 6.1.10                                ...           9 years ago
  • 6.1.5                                ...           9 years ago
  • 6.1.4                                ...           9 years ago
  • 6.0.20                                ...           9 years ago
  • 6.0.15                                ...           9 years ago
  • 6.0.14                                ...           9 years ago
  • 6.0.12                                ...           9 years ago
Downloads
Total 16
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dev Dependencies (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE