Skip to content Skip to sidebar Skip to footer

Get Access To The Class Static Properties Using Babeljs 6

Below is the minimal app which demonstrate the issue: 'use strict'; var _ = require('underscore'); class Model { constructor(value) { this._value = value; }

Solution 1:

The solution was following:

  1. Stick to the new.target as suggested by @sjrd and @loganfsmyth:

    classObjectModelextendsModel
    {
        static properties = {};
    
        constructor(value) {
            super(_.extend({}, new.target.properties, _.pick(value, _.keys(new.target.properties))));
        }
    }
    
  2. Create a transpiler which converts all of new.target (ES6) into this.constructor (ES5):

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

Post a Comment for "Get Access To The Class Static Properties Using Babeljs 6"