1

I'm trying to explore using ES6 classes instead of how we do it currently, using the Function.prototype means. Currently our API looks like:

var myclass = createClass('MyClass', {
    test : function() {}
});

We iterate through the object and apply those properties onto the Function that we return, basically a prettier way than to do so that it's more inline with other programming languages of sorts:

function MyClass() {}

MyClass.prototype.test = function() {};

We also cache the class onto an object where name is the key and the function is the value for use throughout our application. The class name can be namespaced so you can have My.Cls and it will split by the period and then cache it onto the manager but it also can be retrieved via window.My.Cls.

Looking into ES6 classes, I don't see how I can keep the createClass function. Would love something like:

function createClass(name, config) {
    return class name config;
}

I didn't expect it to work and it doesn't.

Two issues I have here:

  1. How can I create a class using a variable as the class name?
  2. How can I create a class and assign the properties via the config object argument?

Not sure this would be possible. We don't plan on keeping the createClass, we hope to keep it for now and upgrade our legacy "classes". I'd like to start using ES6 classes but not break the whole app for however long it'll take us to fully upgrade.

T3KBAU5
  • 1,861
  • 20
  • 26
Mitchell Simoens
  • 2,516
  • 2
  • 20
  • 29

1 Answers1

0

The only good upgrade route is to refactor the property hashes into proper classes. You can start that work and keep using your hash-based classes in the meantime, which will lighten the requirement to do it all at once.

If you have a limited number of "class" name:config pairs -- which you should for maintainability reasons -- then you can replace createClass with an implementation that does:

class Foo { ... }
class Bar { ... }

let classes = {'Foo': Foo, 'Bar': Bar};
function createClass(name, config) {
  if (classes[name]) {
    return classes[name];
  }
  // old impl
}

This will ignore the config if a "real" implementation exists, but keep using the legacy behavior if you haven't replaced the class. If it is, you can implement createClass more like:

function createClass(name, config) {
  if (classes[name]) {
    return new classes[name](config);
  }
  // old impl
}

and pass the config arguments into the class ctor. In this case, you may want to filter out function properties (methods) first, as the class probably implements them already. Something like:

function createClass(name, config) {
  if (classes[name]) {
    let fields = Object.keys(config).filter(key => {
      return typeof config[key] !== 'function';
    }).map(key => config[key]);

    return new classes[name](fields);
  }
  // old impl
}
ssube
  • 47,010
  • 7
  • 103
  • 140
  • Yeah, kind of figured there is no way to do this. Was hoping to not have our current implementation of the class system mixed in but not surprising my edge case isn't supported. – Mitchell Simoens May 11 '15 at 14:42