NecreauxNullPoiиteя提出了一个问题:How do I change the value of a global variable inside of a function,或许与您遇到的问题类似。
回答者Spudley给出了该问题的处理方式:
Just reference the variable inside the function; no magic, just use it's name. If it's been created globally, then you'll be updating the global variable.
You can override this behaviour by declaring it locally using var, but if you don't use var, then a variable name used in a function will be global if that variable has been declared globally.
That's why it's considered best practice to always declare your variables explicitly with var. Because if you forget it, you can start messing with globals by accident. It's an easy mistake to make. But in your case, this turn around and becomes an easy answer to your question.
希望本文对你有帮助,欢迎支持JavaScript中文网