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

JavaScript scope chain example sharing_javascript skills

WBOY
Release: 2016-05-16 16:46:36
Original
1043 people have browsed it

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
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template