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

发布于 2024-11-14 20:41:39 字数 458 浏览 3 评论 0原文

我有以下 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 存储中获取此值。

我做错了什么?

I have the following javascript code:

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 currentIds hasn't got any value it always executes else statement.

UPDATE:

I'm getting this value from HTML5 storage.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

转身以后 2024-11-21 20:41:39

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

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'); 返回字符串“undefined”

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

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".

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

蒗幽 2024-11-21 20:41:39

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

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

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

if ((currentIds === undefined) || (currentIds === null)) {
  //...
日裸衫吸 2024-11-21 20:41:39

它不起作用,因为 localStorage.getItem 返回 null 如果该项目未定义,则不会返回 undefined http://dev.w3.org/html5/webstorage/#dom-storage-getitem

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

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

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

因此你应该正在测试

alert(notStored === null);

It's not working because localStorage.getItem returns null if the item is not defined, it does not return undefined http://dev.w3.org/html5/webstorage/#dom-storage-getitem

Example: 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);
挥剑断情 2024-11-21 20:41:39

我认为你必须检查 undefined== 而不是 === 进行比较。
示例:

typeof currentIds == "undefined"

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

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

typeof currentIds == "undefined"

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

谁与争疯 2024-11-21 20:41:39

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


currentIds = "undefined"

意味着

typeof currentIds == "String"

另请参阅,检测未定义,字符串比较不需要===。


[Edit Edit Edit Edit :P]


currentIds = "undefined"

implies

typeof currentIds == "String"

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

暖伴 2024-11-21 20:41:39

就我而言,LocalStorage.getItem() 将其转换为“未定义”字符串。
我还需要检查它是否作为字符串“未定义”。

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

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

var myItem = LocalStorage.getItem('myItem');
if(myItem != "undefined" && myItem != undefined && myItem != null){
    ...
}
妄司 2024-11-21 20:41:39
if( typeof(varName) != "undefined" && varName !== null )

确保在使用 typeof() 检查时使用 (" ") 引号

if( typeof(varName) != "undefined" && varName !== null )

be sure, you use ( " " ) quotes when checking with typeof()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文