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

Executing constructor in class from module doesn't pass arguments #1601

Closed
gfwilliams opened this Issue Jan 21, 2019 · 1 comment

Comments

Projects
None yet
1 participant
@gfwilliams
Copy link
Member

gfwilliams commented Jan 21, 2019

Modules.addCached("mod",`
class b {
  constructor(a) {
    console.log("Argument",a);
    if (a!="Foo") throw "No argument passed!";
  }
}
exports = b;`);

mB = new (require('mod'))("Foo");

Fails, but

class b {
  constructor(a) {
    console.log("Argument",a);
    if (a!="Foo") throw "No argument passed!";
  }
}

mB = new b("Foo");

works, as does:

Modules.addCached("mod",`
function b(a) {
  console.log("Argument",a);
  if (a!="Foo") throw "No argument passed!";
}
exports = b;`);

mB = new (require('mod'))("Foo");

http://forum.espruino.com/conversations/329854/#comment14581420

This worked in 2v00

@gfwilliams

This comment has been minimized.

Copy link
Member Author

gfwilliams commented Jan 21, 2019

Ok, comparing traces of the object it seems this is because in a class the scope gets moved to the front of the function (before parameters), when the order actually matters during execution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.