<分区>
我正在读这个article我有一些问题请问:
考虑这段代码:
1: var a = 1;
2: function b () {
3: a = 10;
4: return;
5: function a() {}
6: }
7: b();
8: alert(a);
这将提醒 1。(我的问题是为什么?)
文章指出它与名称解析有关。
名称解析(根据文章)由以下命令确定:
1. Internal mechanisms of language: for example, in all scopes are available “this” and “arguments”.
2. Formal parameters: the functions can be named as the formal parameters, which scope is limited to the function body.
3. Function declarations: declared in the form of function foo() {}.
4. Variable declarations: for example, var foo;.
第 3 行假设更改全局 a 的值。 但是函数 a(){...} 优先于内部声明(如果我理解正确的话) 这就是为什么警报 1
附注如果我删除第 5 行,它会提醒第 10 行。
In general, if a name is already defined, it will never be redefined by another entity with the same name. That is the function declaration has a priority over the declarations of the variable with the same name. But this does not mean that the value of a variable assignment does not replace the function, just its definition will be ignored.
我不明白那部分:
But this does not mean that the value of a variable assignment does not replace the function
请问 2 个问题:
我是否正确理解了发出警报的原因 1
上面一行是什么意思? (被误解的部分)
谢谢。