使用'var undefined'来测试undefined是否有任何问题(Is there anything wrong with using 'var undefine

编程入门 行业动态 更新时间:2024-10-19 08:44:32
使用'var undefined'来测试undefined是否有任何问题(Is there anything wrong with using 'var undefined' to test for undefined)

这个测试对于undefined有什么问题吗?

var undefined; if(x == undefined){ //do something }

或这个:

function undefined(x){ return typeof x == 'undefined'; } if(undefined(x)){ //do something }

jsLint不会抛出存储的单词错误,但代码似乎仍然工作...

Is there anything wrong with this test for undefined?

var undefined; if(x == undefined){ //do something }

or this:

function undefined(x){ return typeof x == 'undefined'; } if(undefined(x)){ //do something }

jsLint doesn't throws a reserverd word error, but the code still seems to work...

最满意答案

不要重新定义undefined 。

其他程序员期望undefined总是undefined ,而不是函数的功能。

人们经常使用typeof运算符来确保在用于测试undefined变量时引用错误不会被抛出。

如果有人向你这样做,你可以使用...

undefined = void 0;

...将其还原。

Don't redefine undefined.

Other programmers expect undefined to always be undefined, not a function for function's sake.

People often use typeof operator to ensure a reference error is not thrown when used to test for variables that are undefined.

If anyone ever does this to you, you can use...

undefined = void 0;

... to revert it back.

更多推荐

本文发布于:2023-04-29 02:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334519.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有任何   测试   undefined   var   wrong

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!