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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6295665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:13:11  来源:igfitidea点击:

Checking undefined value not working?

javascriptnullundefined

提问by VansFannel

I have the following javascript code:

我有以下 javascript 代码:

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

if ((typeof currentIds === "undefined") ||
    (currentIds == null))
        $.myNameSpace.currentIDs = new Array(3);
    else
        $.myNameSpace.currentIDs = currentIds.Split(',');

I'm debugging with Firebug and although currentIdshasn't got any value it always executes elsestatement.

我正在用 Firebug 进行调试,虽然currentIds没有任何价值,但它总是执行else语句。

UPDATE:

I'm getting this value from HTML5 storage.

更新:

我从 HTML5 存储中获取此值。

What am I doing wrong?

我究竟做错了什么?

回答by VansFannel

This is how I have solved my problem:

这是我如何解决我的问题:

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');returns the string "undefined".

localStorage.getItem('currentPairsIds');返回字符串"undefined"

There is another error in Split()function. The right version is without any capital letter.

功能上还有一个错误Split()。正确的版本没有任何大写字母。

回答by maerics

I would use a direct comparison instead of the notoriously odd "typeof" operator:

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

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

回答by Juan Mendes

It's not working because localStorage.getItemreturns nullif the item is not defined, it does not return undefinedhttp://dev.w3.org/html5/webstorage/#dom-storage-getitem

这不是因为工作localStorage.getItem的回报null,如果没有定义的项目,它没有返回undefinedhttp://dev.w3.org/html5/webstorage/#dom-storage-getitem

Example: http://jsfiddle.net/mendesjuan/Rsu8N/1/

示例:http: //jsfiddle.net/mendesjuan/Rsu8N/1/

var notStored = localStorage.getItem('ffff');

alert(notStored); // null
alert(typeof notStored); // object, yes, null is an object.

Therefore you should just be testing

因此你应该只是测试

alert(notStored === null);

回答by Andrew



[Edit Edit Edit Edit :P]

[编辑编辑编辑编辑:P]



currentIds = "undefined"

implies

暗示

typeof currentIds == "String"

Also see, Detecting Undefined, === isn't necessary for string comparison.

另请参阅,检测未定义,=== 不是字符串比较所必需的。

回答by Nik Sumeiko

I think you have to make checking for undefinedcomparing with ==instead of ===. Example:

我认为您必须检查undefined==而不是===. 例子:

typeof currentIds == "undefined"

This will make sure, the variable is really undefined or not.

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

回答by bthn

In my case LocalStorage.getItem() was converting it to "undefined" as string. I also needed to check if it s "undefined" as a string.

在我的情况下 LocalStorage.getItem() 将其转换为“未定义”作为字符串。我还需要检查它是否“未定义”为字符串。

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