如何测试Javascript中的变量是否已初始化? [英] How do I test if a variable in Javascript is initialized?

查看:58
本文介绍了如何测试Javascript中的变量是否已初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何测试我的javascript代码中的变量是否已初始化?

How do I test if a variable in my javascript code is initialized?

此测试应返回false

This test should return false for

var foo;

var foo = 5;


推荐答案

if (foo === undefined) { /* not initialized */ }

或偏执狂

if (foo === (void) 0)

这是你可以在JavaScript控制台中测试的东西。只需声明一个变量,然后将其用于(嗯, as )一个表达式。控制台打印的内容是您需要做的一个很好的提示。

This is the sort of thing that you can test right in your JavaScript console. Just declare a variable and then use it in (well, as) an expression. What the console prints is a good hint to what you need to do.

这篇关于如何测试Javascript中的变量是否已初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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