Svelte: Cannot access 'variable_name' before initialization

Created on 12 Jul 2019  路  4Comments  路  Source: sveltejs/svelte

I'm running into this issue. Is this a bug?

Is using let for reactive variables not allowed?

https://stackoverflow.com/questions/56318460/cannot-access-variable-name-before-initialization

pending clarification

Most helpful comment

I'm afraid that's regular javascript behavior.

Variables in an inner scope shadow/override variables with the same name in outer scopes. There's no way to reference the shadowed outer scope variable in those cases, so name really refers to the function's variable.

On top of that, with let and const you're subject to the temporal dead zone, and that's why you get ReferenceErrors: you're trying to use the name variable in its own initialization, ie. before the variable is initialized.

I don't see anything actionable from Svelte's side on this one, unless I misunderstood your issue!

All 4 comments

The issue I ran into was different from the stackoverflow issue.

I had a function use the same variable name as the reactive variable:

https://svelte.dev/repl/8e15e373f14a457792b3cb0fdf2be498?version=3.6.7

I'm afraid that's regular javascript behavior.

Variables in an inner scope shadow/override variables with the same name in outer scopes. There's no way to reference the shadowed outer scope variable in those cases, so name really refers to the function's variable.

On top of that, with let and const you're subject to the temporal dead zone, and that's why you get ReferenceErrors: you're trying to use the name variable in its own initialization, ie. before the variable is initialized.

I don't see anything actionable from Svelte's side on this one, unless I misunderstood your issue!

I agree. There's nothing for Svelte to do if this is normal Javascript behavior.

just linking a possible terser bug for future people googling this problem https://github.com/sveltejs/template/issues/81 - fixed my issue, could fix yours

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rob-balfre picture rob-balfre  路  3Comments

mmjmanders picture mmjmanders  路  3Comments

juniorsd picture juniorsd  路  3Comments

Rich-Harris picture Rich-Harris  路  3Comments

noypiscripter picture noypiscripter  路  3Comments