JavaScript only has function scope; each function has a scope chain directly to the window object.
Variables are searched layer by layer from the inside out until found.
At the same time, not only can you find and use it, but you can even change external variables.
var color = "blue";
function changeColor () {
var anotherColor = "red";
function swapColors() {
var tempColor = anotherColor;
anotherColor = color;
color = tempColor;
}
swapColors();
}
changeColor();
console.log(color); // "red" External variables can not only be accessed but also modified
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn