Hey everyone! I'm having a frustrating issue with my JavaScript app. The setup involves an `index.html` file that loads an `app.js` script, which then dynamically adds a `plugin.js` script. The plugin is supposed to call a register method on the window for some callbacks. The weird part is during the callback execution. For example, I have a callback function that takes a parameter `s`. When I log `s`, it shows 'hello', but when I try to get its length, I get an error saying `s` is undefined. This happens even when the debugger is paused, ruling out any asynchronous timing issues. I've checked everything I could think of. Anyone have an idea what could be causing this?
4 Answers
Sounds strange! Have you tried logging `s` and its properties just to see what exactly is being passed? Sometimes, there might be unexpected issues that can be revealed through logging. It might be worth looking at the actual content.
Really weird! One last thing to check: do you have another variable `s` declared somewhere in your global scope? Sometimes shadowing can cause these kinds of issues.
No, I double-checked that. Tried multiple callbacks too, and 's' was just a placeholder for this example.
It might be helpful to check what's actually being executed in your code as opposed to just relying on the source. It sounds like there could be something funky with how `const l = s.length` is being transpiled to JavaScript.
Could it be an issue with objects passed by reference, particularly if a third-party library is modifying or deleting it? Just a theory, but it could explain the odd behavior you're seeing.
Yeah, I did log `s`. It shows up as a normal string, so at least `typeof s` confirms it.