如何停止在 Node.js 中出现此 ReferenceError?
183| });
184|
>> 185| <% if(just_registered) { %>
186| alert("Welcome!");
187| <% } %>
188|
just_registered is not defined
基本上,我想说:如果 just_registered 已定义并且为 true,则发出警报。但是,我想将所有内容设置为 false...我只想将其保留为未定义(我有大约 100 个变量)
183| });
184|
>> 185| <% if(just_registered) { %>
186| alert("Welcome!");
187| <% } %>
188|
just_registered is not defined
Basically, I want to say: if just_registered is defined and is true, then alert. However, I want want to set everything to false...I just want to leave it undefined (i have like 100 variables)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
<% if(typeof just_registered !== "undefined") { %>
基本上是检查局部变量是否存在。为此,您必须使用
typeof
运算符,因为访问未声明的局部变量just_registered
会产生引用错误。Will相比,这是最好的,
与vsWhere
as
因为使用 typeof 运算符访问未声明的变量只会返回
"undefined"
而不是抛出ReferenceError
<% if(typeof just_registered !== "undefined") { %>
Basically your checking whether a local variable exists. To do this you have to use the
typeof
operator since accessingjust_registered
which is an undeclared local variable creates a reference error.This is best compared to
vs
Where as
Will work because accessing an undeclared variable with the typeof operator just returns
"undefined"
rather then throwing aReferenceError