使用BabelJS 6访问类静态属性

时间:2015-11-27 16:34:38

标签: javascript babeljs

以下是展示此问题的最小应用:

'use strict';

var _ = require('underscore');

class Model
{
    constructor(value) {
        this._value = value;
    }

    get value() {
        return this._value;
    }

    toJS() {
        return this.value;
    }
}

class ObjectModel extends Model
{
    static properties = {};

    constructor(value) {
        super(_.extend({}, new.target.properties, _.pick(value, _.keys(new.target.properties))));
    }
}

class PostModel extends ObjectModel
{
    static properties = {
        title: 'Hello'
        /* content: '<p>Lorem ipsum dolor sit amet</p>' */
    };
}

console.log(new PostModel({title: 'Nice day', aa: 11, bb: 22}).toJS());

它应该产生{title: 'Nice day'}。相反,它甚至没有编译。我明白了:

$ babel app.js
SyntaxError: app.js: 'this' is not allowed before super()

我理解为什么这样做是为了对象属性。但我不明白为什么这也是为类变量做的。

在BabelJS 5中,我使用了这个工作:

class ObjectModel extends Model
{
    static properties = {};

    constructor(value) {
        if (0) { super(); }
        super(_.extend({}, this.constructor.properties, _.pick(value, _.keys(this.constructor.properties))));
    }
}

在版本6中,它会编译,但在运行时会产生错误:

Uncaught TypeError: Cannot read property 'constructor' of undefined

在调用super之前,是否有某种方法可以访问类静态变量?使用init()而不是constructor之类的内容不是一种选择。也许创建自定义转换插件?

系统详情:

$ babel --version
6.2.0 (babel-core 6.2.1)

$ cat .babelrc
{
    "presets": ["es2015", "stage-1"]
}

$ babel-doctor

Babel Doctor
Running sanity checks on your system. This may take a few minutes...

✔ Found config at /path/to/.babelrc
✔ No duplicate babel packages found
✔ All babel packages appear to be up to date
✔ You're on npm >=3.3.0

Everything looks all right!

1 个答案:

答案 0 :(得分:0)

解决方案如下:

  1. 按照@sjrd@loganfsmyth的建议坚持new.target

    class ObjectModel extends Model
    {
        static properties = {};
    
        constructor(value) {
            super(_.extend({}, new.target.properties, _.pick(value, _.keys(new.target.properties))));
        }
    }
    
  2. 创建一个将所有new.target(ES6)转换为this.constructor(ES5)的转换器:

    function transpileNewTarget()
    {
        return {
            visitor: {
                MetaProperty(path) {
                    if (path.isMetaProperty() && path.node.meta.name == 'new' && path.node.property.name == 'target') {
                        path.replaceWithSourceString('this.constructor');
                    }
                }
            }
        };
    }