Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
OJezu opened this issue Mar 29, 2016 · 2 comments
Open

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

OJezu opened this issue Mar 29, 2016 · 2 comments

Comments

@OJezu
Copy link
Contributor

OJezu commented Mar 29, 2016

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".

OJezu pushed a commit to OJezu/wire that referenced this issue Apr 11, 2016
OJezu pushed a commit to OJezu/wire that referenced this issue Apr 12, 2016
OJezu pushed a commit to OJezu/wire that referenced this issue Apr 12, 2016
OJezu pushed a commit to OJezu/wire that referenced this issue Apr 23, 2016
@ribaptista
Copy link

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
Copy link

Any idea when will it start supporting ES6 classes ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants