Javascript 改变var值的函数

Javascript 改变var值的函数,javascript,Javascript,有人能解释为什么我得到不同的输出吗 代码1: var a=1; 函数b(){ a=10; console.log(a);//输出10 } b(); 控制台日志(a)//输出10我想说案例1是非常不言自明的,因为a的值被a=10覆盖 在案例2中,由于函数a(){}和提升的原因,变量声明和函数定义被上移到最近的词法范围,即函数a()的函数b(),因此函数a的作用范围是函数b()并且更改其值不会影响全局a的值,而是覆盖函数a() 正因为如此,console.log(a)出现在函数b()之外(正在记录

有人能解释为什么我得到不同的输出吗

代码1:

var a=1;
函数b(){
a=10;
console.log(a);//输出10
}
b();

控制台日志(a)//输出10
我想说案例1是非常不言自明的,因为
a
的值被
a=10
覆盖

在案例2中,由于
函数a(){}
提升
的原因,变量声明和函数定义被上移到最近的词法范围,即
函数a()
函数b()
,因此
函数a
的作用范围是
函数b()
并且更改其值不会影响全局
a
的值,而是覆盖
函数a()


正因为如此,
console.log(a)
出现在
函数b()之外(
正在记录
1
,因为全局值没有改变。

我相信我们的OP已经尝到了互联网学习的权衡。可访问的信息片段,但没有指南。不幸的是,我们不知道你知道多少,我们假设了很多事情,而那些是你所知道的很少的事情,这些都是先前的知识,最终导致了对什么是可怕的
起重
的理解。这是典型的,在此,我将提供一些关键字,作为您理解JavaScript的指南

创建阶段
+
激活又称代码执行阶段
=
执行上下文

好的,但是每一个都是什么

范围链
+
创建参数、函数、变量
+
此关键字的值
=
创建阶段

为函数分配值和引用,然后执行代码
=
激活或代码执行阶段

  • 创作阶段

    globalExeContext = {
      //no scope
      objForVariables = {
        //no arguments because its no function
        // function declarations
        b: `points to function`,
        // variable declarations
        a: `undefined`
      },
      this: //not important for this example
    }
    
    globalExeContext = {
      //no scope
      objForVariables = {
        //no arguments because its no function
        // function declarations
        b: `points to function`,
        // variable declarations
        a: 1 // because we did the `=` on line 1
      },
      this: //not important for this example
    }
    
    bExeContext = {
      // This is the scope object, and in this object now is placed the global exe context we have worked on before
      scope: {globalExeContext}
      objForVariables = {
        //the is arguments object for this one but its empty, because we have no arguments for this function
        args:{},
        // function declarations are none here
        // variable declarations are none here
      },
      this: //not important for this example
    }
    
    globalExeContext = {
      //no scope
      objForVariables = {
        //no arguments because its no function
        // function declarations
        b: `points to function`,
        // variable declarations
        a: 10 // look what you have done
      },
      this: //not important for this example
    }
    
    bExeContext = {
      scope: {globalExeContext}
      objForVariables = {
        //the is arguments object for this one but its empty, because we have no arguments for this function
        args:{},
        // function declarations
        a: `points to function`
        // variable declarations are none here
      },
      this: //not important for this example
    }
    
    • 设置范围链
    • 创建将包含变量的对象
      • 创建参数对象
      • 搜索函数声明,并为每个声明在上面的对象上创建具有该名称的属性,如果函数名称存在,则将覆盖ref指针值
      • 搜索变量声明,并为每个声明在上面的对象中创建具有该名称的属性,同时将其值设置为
        undefined
        。如果名称已存在,则不执行任何操作
    • 关键字的值已解析
  • 执行阶段也称为运行代码阶段

    • 执行函数exe
      ()
      并将值
      分配给此=分配值
代码1开始按照上面的算法逐行执行

第一阶段:创作阶段

globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: `undefined`
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 1 // because we did the `=` on line 1
  },
  this: //not important for this example
}
bExeContext = {
  // This is the scope object, and in this object now is placed the global exe context we have worked on before
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations are none here
    // variable declarations are none here
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 10 // look what you have done
  },
  this: //not important for this example
}
bExeContext = {
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations
    a: `points to function`
    // variable declarations are none here
  },
  this: //not important for this example
}
完成了创作阶段。我们按照上面定义的说明逐行操作。现在我们正处于执行阶段,分配
=
并执行
()
。我们再次从第1行开始

第二阶段:执行阶段

globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: `undefined`
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 1 // because we did the `=` on line 1
  },
  this: //not important for this example
}
bExeContext = {
  // This is the scope object, and in this object now is placed the global exe context we have worked on before
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations are none here
    // variable declarations are none here
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 10 // look what you have done
  },
  this: //not important for this example
}
bExeContext = {
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations
    a: `points to function`
    // variable declarations are none here
  },
  this: //not important for this example
}
然后在第8行,我们找到了函数执行,这意味着在全局执行上下文的基础上创建新的执行上下文,这意味着我们对该函数再次遵循上面的算法

第一阶段:创作阶段

globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: `undefined`
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 1 // because we did the `=` on line 1
  },
  this: //not important for this example
}
bExeContext = {
  // This is the scope object, and in this object now is placed the global exe context we have worked on before
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations are none here
    // variable declarations are none here
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 10 // look what you have done
  },
  this: //not important for this example
}
bExeContext = {
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations
    a: `points to function`
    // variable declarations are none here
  },
  this: //not important for this example
}
现在,我们已经进入了执行阶段,在这个阶段我们执行
()
=
。在第1行,我们被告知
a=10
,这意味着我们需要找到
a
来为其赋值。但是在
bExeContext
对象中没有
a
?现在怎么办?现在我们进入
范围
并尝试在其中查找它。果然,在我们的全局空间中有一个
a
,现在我们给它赋值10。我们已经覆盖了它。它将永远不会是一样的,我将带回globalExeContext向您展示这一点

第二阶段:执行阶段

globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: `undefined`
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 1 // because we did the `=` on line 1
  },
  this: //not important for this example
}
bExeContext = {
  // This is the scope object, and in this object now is placed the global exe context we have worked on before
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations are none here
    // variable declarations are none here
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 10 // look what you have done
  },
  this: //not important for this example
}
bExeContext = {
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations
    a: `points to function`
    // variable declarations are none here
  },
  this: //not important for this example
}
现在这个问题已经解决了,我们返回执行
b
函数中的下一行,即
console.log(a)。我们需要解决什么是
a
,然后再进行一遍,在
b
中搜索什么都不会给我们,但我们在GlobalExtack中找到了它。您记得,它是
10
。所以我们记录
10
。我们已经到达函数的末尾,它从堆栈中弹出。不再是了

现在我们继续执行全局代码的最后一行:
console.log(a)很简单,GlobalExtack中有一个
a
。它是
10

代码2第1阶段:创建阶段

globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: `undefined`
  },
  this: //not important for this example
}
阶段2:执行阶段
=
()
第一行是一样的

globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 1
  },
  this: //not important for this example
}
第8行指示执行
b
函数

第一阶段:创作阶段

globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: `undefined`
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 1 // because we did the `=` on line 1
  },
  this: //not important for this example
}
bExeContext = {
  // This is the scope object, and in this object now is placed the global exe context we have worked on before
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations are none here
    // variable declarations are none here
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 10 // look what you have done
  },
  this: //not important for this example
}
bExeContext = {
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations
    a: `points to function`
    // variable declarations are none here
  },
  this: //not important for this example
}
第二阶段:执行阶段

globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: `undefined`
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 1 // because we did the `=` on line 1
  },
  this: //not important for this example
}
bExeContext = {
  // This is the scope object, and in this object now is placed the global exe context we have worked on before
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations are none here
    // variable declarations are none here
  },
  this: //not important for this example
}
globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 10 // look what you have done
  },
  this: //not important for this example
}
bExeContext = {
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations
    a: `points to function`
    // variable declarations are none here
  },
  this: //not important for this example
}
第1行指示查找
a
并将
10
分配给它。这次我们很幸运,因为在我们的bExeContext中可以找到
a

bExeContext = {
  scope: {globalExeContext}
  objForVariables = {
    //the is arguments object for this one but its empty, because we have no arguments for this function
    args:{},
    // function declarations
    a: 10 // no longer points to `points to function`
    // variable declarations are none here
  },
  this: //not important for this example
}
这意味着我们不必到全球空间去寻找
a
。执行
b
函数的第2行告诉
console.log(a)。我们需要解决什么是
a
,这是肯定的

bExeContext = {
  // blablbalblablbalblablblal     
    // blablbalblablbalblablblalb
    a: 10 // no longer points to `points to function`
    // blablbalblablbalblablblalb
  },
  // blablbalblablbalblablblalb
}
我们确实在bExeContext上找到了它。我们记录
10
。我们完成了
b
函数的执行,并将其从执行堆栈中弹出。不再是了

我们现在继续评估全局代码。我们完成了
b()在第9行,现在我们在第10行<代码>控制台日志(a)
要解决
a
问题,我们必须首先找到它。要刷新globalExeContext的外观,下面是最后一张图片:

globalExeContext = {
  //no scope
  objForVariables = {
    //no arguments because its no function
    // function declarations
    b: `points to function`,
    // variable declarations
    a: 1
  },
  this: //not important for this example
}
果然,
a
1
。我们记录
1
。我们完了

你看,
提升
在你运行JS代码时就实现了。这是一个放置代码位的过程,如果愿意的话,可以对其进行排序。更确切地说是1.2.函数声明3.变量声明。当代码只有几行或一行,或者没有