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

ReferenceError occurs when referencing a class before it's defined #33

Open
RobbieGM opened this issue Jun 7, 2020 · 3 comments
Open
Labels
bug Something isn't working

Comments

@RobbieGM
Copy link

RobbieGM commented Jun 7, 2020

Hi Leonardo,
You mentioned in this stackoverflow discussion that you attempt prevent causing ReferenceErrors by using the typeof operator, but I showed how they can occur anyway and you said "let me check because this seems like a bug". After much head-scratching, I figured out why this is the case.

let x = 8;
console.log(typeof x); // "number"
console.log(typeof y); // "undefined"
console.log(typeof z); // ReferenceError: cannot access variable z before initialization
let z = 9;

If you don't define something at all, the typeof operator will give you "undefined". But in my case the class was defined too late, and that for some reason causes a ReferenceError. I don't know what to use instead of typeof (maybe a try-catch?) but I just wanted to let you know about this weird language kludge and how you could improve your plugin.

@leonardfactory
Copy link
Owner

Ah, damned TDZ. Should dig into this, a try catch seems hard since we should wrap everything within a function. Maybe an helper.

@obedm503
Copy link

a really simple, and maybe naive, solution is to change the declaration type from let to var. var is not constrained to the TDZ semantics.

@RobbieGM
Copy link
Author

Definitely not best to change all lets and consts to vars from the plugin as that may change behavior unexpectedly. Also in my case I used classes and they were transformed to use the let syntax.

@leonardfactory leonardfactory added the bug Something isn't working label Mar 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants