如何在node.js中停止获取此ReferenceError? [英] How do I stop getting this ReferenceError in node.js?

查看:86
本文介绍了如何在node.js中停止获取此ReferenceError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    183|             });
    184| 
 >> 185|             <% if(just_registered) { %>
    186|                 alert("Welcome!");
    187|             <% } %>
    188| 

just_registered is not defined

基本上,我想说:if just_registered已定义且为真,然后是警报。但是,我想要将所有内容设置为false ...我只想保留未定义(我有100个变量)

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)

推荐答案

<%if(typeof just_registered!==undefined){%>

基本上你的检查是否存在局部变量。为此,您必须使用 typeof 运算符,因为访问 just_registered 这是一个未声明的局部变量会创建一个引用错误。

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.

这是最好的

var foo;
if (foo) { }

vs

//var foo;
if (foo) { } // ReferenceError

其中

//var foo
if (typeof foo !== "undefined") { } 

可行,因为使用typeof运算符访问未声明的变量只返回undefined而不是抛出a ReferenceError

Will work because accessing an undeclared variable with the typeof operator just returns "undefined" rather then throwing a ReferenceError

这篇关于如何在node.js中停止获取此ReferenceError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆