Home >Web Front-end >JS Tutorial >JavaScript scope chain example sharing_javascript skills

JavaScript scope chain example sharing_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:46:361052browse

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.

Copy code The code is as follows:

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