我看到下面有人发布的代码。我对它记录的内容感到困惑。它记录函数 a
,而不是200
。为什么?
var a = 1;
(function a() {
a = 200;
console.log(a)
})()
最佳答案
因为被立即调用的函数是命名的,并且该名称不能被重新分配以直接在 IIFE 中引用其他东西。
任何命名的函数表达式也会表现出这种行为。函数名为 a
的函数表达式意味着直接位于函数内部的 a
将始终引用函数本身,即使您尝试重新分配它也是如此。
如果你使用严格模式,你可以将错误显式化:
'use strict';
var a = 1;
(function a() {
a = 200;
console.log(a)
})()
Uncaught TypeError: Assignment to constant variable.
有一个命名函数表达式有点像有
(function a() {
const a = <this function>;
// ...
})()
除了尝试重新分配它只会抛出严格模式。
具体来说,我相信针对此行为的 ECMAScript 5 规范在 SetMutableBinding 中:
- If the binding for N in envRec is a mutable binding, change its bound value to V.
- Else this must be an attempt to change the value of an immutable binding so if S (strict mode being used) if true throw a TypeError exception.
但直接在函数内部,函数名称绑定(bind)是不可变的 - 参见 Function Definition :
The production
FunctionExpression : function Identifier ( FormalParameterListopt ) { FunctionBody }
is evaluated as follows:
Call the CreateImmutableBinding concrete method of envRec, passing the String value of
Identifier
as the argument.
关于javascript - 为什么记录命名的 IIFE 而不是同名变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56882289/