ReferenceError: assignment to undeclared variable "x" - JavaScript 编辑
The JavaScript strict mode-only exception "Assignment to undeclated variable" occurs when the value has been assigned to an undeclared variable.
Message
ReferenceError: assignment to undeclared variable "x" (Firefox) ReferenceError: "x" is not defined (Chrome) ReferenceError: Variable undefined in strict mode (Edge)
Error type
ReferenceError
warning in strict mode only.
What went wrong?
A value has been assigned to an undeclared variable. In other words, there was an assignment without the var
keyword. There are some differences between declared and undeclared variables, which might lead to unexpected results and that's why JavaScript presents an error in strict mode.
Three things to note about declared and undeclared variables:
- Declared variables are constrained in the execution context in which they are declared. Undeclared variables are always global.
- Declared variables are created before any code is executed. Undeclared variables do not exist until the code assigning to them is executed.
- Declared variables are a non-configurable property of their execution context (function or global). Undeclared variables are configurable (e.g. can be deleted).
For more details and examples, see the var
reference page.
Errors about undeclared variable assignments occur in strict mode code only. In non-strict code, they are silently ignored.
Examples
Invalid cases
In this case, the variable "bar" is an undeclared variable.
function foo() {
'use strict';
bar = true;
}
foo(); // ReferenceError: assignment to undeclared variable bar
Valid cases
To make "bar" a declared variable, you can add the var
keyword in front of it.
function foo() {
'use strict';
var bar = true;
}
foo();
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论