183| });
184|
>> 185| <% if(just_registered) { %>
186| alert("Welcome!");
187| <% } %>
188|
just_registered is not defined
基本上,我想说的是:如果 just_registered 被定义并且为真,那么警报。但是,我想将所有内容都设置为 false...我只想保留它未定义(我有 100 个变量)
最佳答案
<% if(typeof just_registered !== "undefined") { %>
基本上是检查局部变量是否存在。为此,您必须使用 typeof
自访问以来的运算符(operator) just_registered
这是一个未声明的局部变量会产生引用错误。
这是最好的对比
var foo;
if (foo) { }
对比
//var foo;
if (foo) { } // ReferenceError
在哪里
//var foo
if (typeof foo !== "undefined") { }
将起作用,因为使用 typeof 运算符访问未声明的变量只会返回 "undefined"
而不是抛出 ReferenceError
关于javascript - 如何停止在 node.js 中获取此 ReferenceError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6645512/