问题
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)
回答1:
<% if(typeof just_registered !== "undefined") { %>
Basically your checking whether a local variable exists. To do this you have to use the typeof
operator since accessing just_registered
which is an undeclared local variable creates a reference error.
This is best compared to
var foo;
if (foo) { }
vs
//var foo;
if (foo) { } // ReferenceError
Where as
//var foo
if (typeof foo !== "undefined") { }
Will work because accessing an undeclared variable with the typeof operator just returns "undefined"
rather then throwing a ReferenceError
来源:https://stackoverflow.com/questions/6645512/how-do-i-stop-getting-this-referenceerror-in-node-js