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

Direct call and indirect call of eval behave differently #327

Open
wheatup opened this issue Jun 26, 2024 · 1 comment
Open

Direct call and indirect call of eval behave differently #327

wheatup opened this issue Jun 26, 2024 · 1 comment
Labels
new-example A proposal of the new example

Comments

@wheatup
Copy link

wheatup commented Jun 26, 2024

When calling eval indirectly, it uses the top scope. This means you can access the top scope variables even in a local scope where the same variable name is redefined. (Note that the variable is defined with let, so it is not a property of the global object.)

let foo = 'outer';

(() => {
  let foo = 'inner';

  console.log(eval('foo'));  // > "inner"
  console.log(eval?.('foo'));  // > "outer"
})();

This behavior is explained in the MDN documentation on eval.

@wheatup wheatup added the new-example A proposal of the new example label Jun 26, 2024
@wheatup wheatup changed the title Direct call and indirect call of eval behaves differently Direct call and indirect call of eval behave differently Jun 26, 2024
@Rudxain
Copy link

Rudxain commented Sep 1, 2024

Peak black magic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-example A proposal of the new example
Projects
None yet
Development

No branches or pull requests

2 participants