typeof!==“未定义” vs. = null

JavaScript

猴子十三

2020-03-12

我经常看到JavaScript代码以这种方式检查未定义的参数等:

if (typeof input !== "undefined") {
    // do stuff
}

这似乎有点浪费,因为它涉及类型查找和字符串比较,更不用说它的冗长了。这是必需的,因为undefined可以重命名。

我的问题是:
与该方法相比,该代码有何优势:

if (null != input) {
    // do stuff
}

据我所知,您无法重新定义null,因此不会意外中断。并且,由于!=运算符的类型强制,这将同时检查undefinednull...,这通常正是您想要的(例如,用于可选功能参数)。

但是,这种形式似乎并不广泛,甚至会导致JSLint对使用邪恶!=运算符的人大吼大叫

为什么认为这种风格不好?

第1291篇《typeof!==“未定义” vs. = null》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

6个回答
猪猪路易 2020.03.12
if (input == undefined) { ... }

works just fine. It is of course not a null comparison, but I usually find that if I need to distinguish between undefined and null, I actually rather need to distinguish between undefined and just any false value, so

else if (input) { ... }

does it.

If a program redefines undefined it is really braindead anyway.

The only reason I can think of was for IE4 compatibility, it did not understand the undefined keyword (which is not actually a keyword, unfortunately), but of course values could be undefined, so you had to have this:

var undefined;

and the comparison above would work just fine.

In your second example, you probably need double parentheses to make lint happy?

小胖泡芙 2020.03.12

var bar = null;
console.log(typeof bar === "object"); //true yes 
//because null a datatype of object

var barf = "dff";
console.log(typeof barf.constructor);//function


console.log(Array.isArray(bar));//falsss


console.log((bar !== null) && (bar.constructor === Object)); //false

console.log((bar !== null) && (typeof bar === "object"));  // logs false
//because bar!==null, bar is a object


console.log((bar !== null) && ((typeof bar === "object") || (typeof bar === "function"))); //false

console.log(typeof bar === typeof object); //false
console.log(typeof bar2 === typeof undefined); //true
console.log(typeof bar3 === typeof undefinedff); //true
console.log(typeof bar2 == typeof undefined); //true

console.log((bar !== null) && (typeof bar === "object") && (toString.call(bar) !== "[object Array]")); //false

王者一打九银 2020.03.12

如果您真的担心未定义会被重新定义,则可以使用以下帮助方法来防止这种情况:

function is_undefined(value) {
   var undefined_check; // instantiate a new variable which gets initialized to the real undefined value
   return value === undefined_check;
}

之所以如此,是因为有人写的时候,undefined = "foo"他只允许名称 undefined引用一个新值,但他不更改的实际值undefined

ALPro 2020.03.12

您还可以使用void运算符获取未定义的值:

if (input !== void 0) {
    // do stuff    
}

(是的,正如在另一个答案中指出的那样,如果未声明变量,这将引发错误,但是这种情况通常可以通过代码检查或代码重构(例如window.input !== void 0用于测试全局变量或添加var input来排除。)

樱猪猪 2020.03.12

好办法:

if(typeof neverDeclared == "undefined") //no errors

但是最好看的方法是通过检查:

if(typeof neverDeclared === typeof undefined) //also no errors and no strings
逆天LEY逆天 2020.03.12

typeof 更安全,因为它允许从未声明过标识符:

if(typeof neverDeclared === "undefined") // no errors

if(neverDeclared === null) // throws ReferenceError: neverDeclared is not defined

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android