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

Javascript global 'let' variable declaration scope bug. #156

Open
ARiedijk opened this issue Jan 17, 2024 · 3 comments
Open

Javascript global 'let' variable declaration scope bug. #156

ARiedijk opened this issue Jan 17, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@ARiedijk
Copy link

A scope issue related to JavaScript variables, particularly with the let keyword, within the context of a function that is modifying the global scope. The parseLetConstGlobals(text) function in is intended to handle global const and let declarations by prefixing them with self., effectively making them properties of the global object (window in a browser). However, this approach leads to unexpected behavior in script.


const globalConst = "const";
let globalLet = "let";
let globalLetChanged = "x";

function loading() {
   console.log("loading" + globalLetChanged);
   globalLetChanged = "changed";
}

window.addEventListener("load", function(){
  loading();
});

This script declares global variables (globalConst, globalLet, globalLetChanged) and a function loading that modifies globalLetChanged. An event listener is attached to the window's load event, which calls loading.

After modification by parseLetConstGlobals function: These variable declarations have a 'self.' addition, this leads to an encapsulation issue.

{
  // ... original script ...
}

self.globalConst = globalConst;
self.globalLet = globalLet;
self.globalLetChanged = globalLetChanged;


Scope Issue: The problem arises because the globalLetChanged variable inside the script and the self.globalLetChanged variable are not the same. When loading modifies globalLetChanged, it only changes the script-local variable, not the global one (self.globalLetChanged). Consequently, any other script that checks the value of globalLetChanged (thinking it's global) finds it unchanged.

@ikreymer
Copy link
Member

Yes, this is still a situation where the fix for webrecorder/wombat#82 is not complete. Don't have a good fix at the moment - I assume you have examples where this is causing a problem? Generally, the need for this is to address 'bad' code which accesses undeclared let globals that were declared in a different file - hopefully such code is on the decrease.

Some options could be injecting the self.X = Y assignment after the initial assignment, or converting to var, but those have other consequences. Would be good to see some real world examples where this is an issue if you have some.

@ARiedijk
Copy link
Author

ARiedijk commented Jan 23, 2024 via email

@Shrinks99 Shrinks99 added the bug Something isn't working label Feb 16, 2024
@ikreymer
Copy link
Member

I hoping that this is something that we will see less and less off, especially with the move to using modules and more strict mode / better code that doesn't rely on using implicit globals from different modules (which is why we need the self. in the first place)

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