我正在阅读这篇文章,我有一些问题请教:
考虑到这段代码:
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
ps如果我删除第5行,它会提醒10。
一般来说,如果一个名字已经被定义,它永远不会被另一个同名的实体重新定义。即函数声明优先于同名变量的声明。但这并不意味着变量赋值不会替换函数,只是它的定义会被忽略。
我不明白那部分:
但这并不意味着变量赋值的值不会替换函数
所以有两个问题:
我是否正确理解了提醒的原因1
上面的线是什么意思?(被误解的部分)
谢谢。