cujojs / wire

A light, fast, flexible Javascript IOC container
Other
861 stars 68 forks source link

Not es6 ready - does not work with es6 classes #181

Open OJezu opened 8 years ago

OJezu commented 8 years ago

If I declare class using es6 class keyword:

class Foo {
    constructor(foo, bar) {}
}
module.exports = Foo;

And I try to load it using wire:

wire({
   "foo": {
        "module": "Foo",
        "args": ["monty", "python"],
    },
});

I get an error:

TypeError: Class constructors cannot be invoked without 'new'
    at BrowserConnectionFactory ()
    at instantiate (.../node_modules/wire/lib/instantiate.js:37:20)
    at createInstance (.../node_modules/wire/lib/plugin/basePlugin.js:250:7)

Wire (actually when) is trying to do Foo.apply(new_foo, args), which is not allowed in es6. Solution would be using new spread operator - new Foo(...args) , but that is not backwards-compatible. PR #180 has possible workaround with named constructors which can be called with "apply".

ribaptista commented 8 years ago

For now, you could export a factory method instead of class itself:

class Foo {
    constructor(foo, bar) {}
    static create(foo, bar) {
        return new Foo(foo, bar);
    }
}
module.exports = Foo.create;
vipreshjha commented 7 years ago

Any idea when will it start supporting ES6 classes ?