How do I stop getting this ReferenceError in node.js?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 19:02:45

问题


    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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!