本文介绍了为什么在函数内部创建同名变量时不会覆盖函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var a = 'why is this not undefined?';
function checkScope(a) {
    var a;
    console.log(a);
}
checkScope(a);

Javascript是功能范围语言,对吧?当我在与函数参数使用相同名称的函数内部声明一个新变量时,为什么新定义的变量仍然保存与参数相同的数据?

Javascript is functional scope language, right? When I declare a new variable just inside the function that uses the same name as the functional argument, why does the newly defined variable still hold the same data as the argument?

我认为它应该是未定义的?

I thought it should be undefined?

推荐答案

var a; 实际上是变量声明语句。定义函数时,在代码执行之前处理其中声明的所有变量,因此即使在运行时执行实际声明行之前也可以使用变量。这称为。因此,无论您声明一个变量多少次,该变量实际上只被声明一次。

var a; is actually a variable declaration statement. When the function is defined, all the variables declared in it, are processed before the code execution and so you can use the variables even before the actual declaration line is executed at runtime. This is called var hoisting. So, no matter how many times you declare a variable, the variable is actually declared only once.

在您的情况下,您定义了 a 作为函数的参数之一,其范围限定为当前函数。然后你声明一个具有相同名称的变量。由于 a 已在函数中声明,作为其中一个参数, var a; 声明将被忽略。

In your case, you have defined a as one of the parameters to the function, which is scoped to the current function. And then you are declaring a variable with the same name. Since a is already declared in the function, as one of the parameters, the var a; declaration will be ignored.

这就是为什么你得到为什么这不是未定义的?在控制台中。

That is why you are getting why is this not undefined? in the console.

而不是 var a; ,假设你有 var a = 1; ,在这种情况下,变量已经声明,但赋值表达式将在运行时评估,值 1 将被分配给 A 。因此, console.log 将打印 1

Instead of var a;, lets say you had var a = 1;, in this case, the variable is already declared but the assignment expression will be evaluated at runtime and the value 1 will be assigned to a. So, the console.log will print 1.

此行为在ECMA脚本5.1规范的,

This behaviour is explained in the ECMA Script 5.1 Specification, under the section 10.5 Declaration Binding Instantiation,


  1. 如果代码是功能代码,然后

a。设func是[[Call]]内部方法启动代码执行的函数。让名称 func
[[FormalParameters]]内部属性的值。

a. Let func be the function whose [[Call]] internal method initiated execution of code. Let names be the value of func’s [[FormalParameters]] internal property.

湾设 argCount 为args中的元素数。

b. Let argCount be the number of elements in args.

c。设 n 为数字0。

d。对于名称中的每个String argName ,按列表顺序执行

d. For each String argName in names, in list order do

       我。设 n n 的当前值加1。

      i. Let n be the current value of n plus 1.

       ⅱ。如果 n 大于 argCount ,请将 v 取消定义,否则让 v 的值n args的元素

      ii. If n is greater than argCount, let v be undefined otherwise let v be the value of the n’th element of args.

       iii。 argAlreadyDeclared 是调用 env 的HasBinding具体方法传递 argName 作为参数的结果。

      iii. Let argAlreadyDeclared be the result of calling env’s HasBinding concrete method passing argName as the argument.

       iv。 如果 argAlreadyDeclared 为false,请调用 env 的CreateMutableBinding具体方法,将 argName 作为
参数传递。

      iv. If argAlreadyDeclared is false, call env’s CreateMutableBinding concrete method passing argName as the argument.

       v。调用 env 的SetMutableBinding具体方法,将 argName v strict 作为参数传递。

      v. Call env’s SetMutableBinding concrete method passing argName, v, and strict as the arguments.


....


  1. 对于代码中的每个 VariableDeclaration VariableDeclarationNoIn d >,在源文本顺序中

  1. For each VariableDeclaration and VariableDeclarationNoIn d in code, in source text order do

a。让 dn 成为 d 中的标识符

b。让 varAlreadyDeclared 成为调用 env 的HasBinding具体方法传递 dn 作为参数的结果。

b. Let varAlreadyDeclared be the result of calling env’s HasBinding concrete method passing dn as the argument.

c。 如果 varAlreadyDeclared 为false,则

c. If varAlreadyDeclared is false, then

       i。 调用 env 的CreateMutableBinding具体方法,将 dn configurableBindings 作为参数传递。

      i. Call env’s CreateMutableBinding concrete method passing dn and configurableBindings as the arguments.

       ii。调用 env 的SetMutableBinding具体方法,将 dn ,undefined和 strict 作为参数传递。

      ii. Call env’s SetMutableBinding concrete method passing dn, undefined, and strict as the arguments.


正如我们在规范中看到的那样,在函数中声明的参数和变量,都是在执行环境中实际定义的到它们被定义的功能。因此,如果参数和变量具有相同的名称,则该变量实际上只定义一次,而忽略第二个声明。

As we see in the specification, the arguments and the variables declared in the function, all are actually defined in the execution environment corresponding to the function in which they are defined. So, if the arguments and the variables have the same name, the variable is actually defined only once and the second declaration is ignored.

这篇关于为什么在函数内部创建同名变量时不会覆盖函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:27