Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查未定义的值不起作用?_Javascript_Null_Undefined - Fatal编程技术网

Javascript 检查未定义的值不起作用?

Javascript 检查未定义的值不起作用?,javascript,null,undefined,Javascript,Null,Undefined,我有以下javascript代码: var currentIds = localStorage.getItem('currentPairsIds'); if ((typeof currentIds === "undefined") || (currentIds == null)) $.myNameSpace.currentIDs = new Array(3); else $.myNameSpace.currentIDs = currentIds.

我有以下javascript代码:

var currentIds = localStorage.getItem('currentPairsIds');

if ((typeof currentIds === "undefined") ||
    (currentIds == null))
        $.myNameSpace.currentIDs = new Array(3);
    else
        $.myNameSpace.currentIDs = currentIds.Split(',');
我正在用Firebug进行调试,虽然
currentIds
没有任何值,但它总是执行
else
语句

更新:

我从HTML5存储中获得这个值

我做错了什么?


[编辑:p]


暗示

typeof currentIds == "String"

另请参见,===对于字符串比较不是必需的

我认为您必须检查
未定义的
=
相比,而不是
=
。 例如:

typeof currentIds == "undefined"

这将确保变量是否真的未定义。

我将使用直接比较,而不是臭名昭著的“typeof”运算符:

if((currentIds==undefined)| |(currentIds==null)){
//...

它不工作,因为
localStorage.getItem
返回
null
如果未定义项,则不会返回
未定义的

例如:

因此,您应该只进行测试

alert(notStored === null);

这就是我解决问题的方法:

var currentIds = localStorage.getItem('currentPairsIds');

if ((currentIds === undefined) ||
    (currentIds == null) || (currentIds == "undefined"))
        $.myNameSpace.currentIDs = new Array(3);
    else
        $.myNameSpace.currentIDs = currentIds.split(',');
localStorage.getItem('currentPairsIds');
返回字符串
“未定义”


Split()
函数中还有一个错误。正确的版本没有任何大写字母。

在我的例子中,LocalStorage.getItem()将其转换为字符串形式的“未定义”。 我还需要检查它是否作为字符串“未定义”

var myItem = LocalStorage.getItem('myItem');
if(myItem != "undefined" && myItem != undefined && myItem != null){
    ...
}


请确保,在使用
typeof()

检查时使用(“”)引号,这一定意味着
currentIds
的值不是真正的
未定义的
@Pointy:它是未定义的。语句
$.myNameSpace.currentIds=currentIds.Split(',');
抛出异常。是
currentIds
还是
未定义?
类型的“未定义”
is string.currentIds没有任何值。要么你被困在另一个世界中,要么currentIds的值不是你认为的值。你的代码是正确的,currentIds的值正如你所描述的,似乎与你期望的不符,问题出在其他地方。我已经更新了我的问题这行
var currentIds=localStorage.getItem('currentPairsIds');
这是我获取
currentIds
值的地方。@VansFannel您正在检索一个数组,然后试图对其调用split,该数组未定义。您以前的答案是正确的。localStorage.getItem返回字符串“未定义”。我必须检查currentIds是否=“未定义”@Andrew:但我已经用currentIds==“undefined”解决了。你的答案是currentIds=“undefined”。
=
==
在这种情况下没有任何区别。这不会改变什么,typeof返回一个字符串。这是错误的,
localStorage.getItem()
不会返回字符串“undefined”,它返回
null
,您只需要
null
检查,而不是盲目测试的其他内容。@JuanMendes,我得到了一个未定义的语法,这个语法解决了我的问题。事实上,这是一个奇怪的
=“未定义”
这抓住了我的问题!@DaveA这是因为您保存了一些未定义的内容,而本地存储将其转换为字符串“未定义”。将某些内容保存到localStorage时,应小心不要将其设置为未定义或null。我会始终将其设置为空字符串instead@JuanMendes,我确实保存了无效数据。应用程序处于早期开发阶段,出现了草率的情况。但我不认为字符串“未定义”进入内存。但类似的事情会发生。您可以通过验证自杀,但问题会爬过您…@DaveA
localStorage.setItem('test',undefined)
将导致
localStorage.getItem('test'))
返回
'undefined'
字符串。如果将其设置为错误值,则很容易发生这种情况。这是您应该避免的
var currentIds = localStorage.getItem('currentPairsIds');

if ((currentIds === undefined) ||
    (currentIds == null) || (currentIds == "undefined"))
        $.myNameSpace.currentIDs = new Array(3);
    else
        $.myNameSpace.currentIDs = currentIds.split(',');
var myItem = LocalStorage.getItem('myItem');
if(myItem != "undefined" && myItem != undefined && myItem != null){
    ...
}
if( typeof(varName) != "undefined" && varName !== null )