JavaScript scoped chain sample sharing

  • 2020-03-30 03:06:33
  • OfStack

JavaScript has only function scope; Each function has a chain of scopes leading to the window object.

Variables are searched from the inside out, while they are found.

At the same time, you can not only find and use it, you can even change external variables.

var color = "blue";
function changeColor() {
    var anotherColor = "red";
    function swapColors() {
        var tempColor = anotherColor;
        anotherColor = color;
        color = tempColor;
console.log(color);  //"Red"   External variables can be accessed as well as modified

Related articles: