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

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

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

我的问题是: 这段代码比下面的方法好到哪里去了:

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

据我所知,你不能重定义null,所以它不会意外中断。并且,由于!=操作符的类型强制,这将检查undefined和null…这通常正是你想要的(例如,对于可选的函数参数)。

然而,这种形式似乎并不普遍,它甚至会导致JSLint因为您使用了邪恶的!=操作符而对您大喊大叫。

为什么这被认为是糟糕的风格?


当前回答

好的方法:

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

但最好看的方法是通过以下方式查看:

if(typeof neverDeclared === typeof undefined) //also no errors and no strings

其他回答

我实际上遇到过if (typeof input !== 'undefined')在这个场景中,它被用来提供默认的函数参数:

function greet(name, greeting) {
  name = (typeof name !== 'undefined') ?  name : 'Student';
  greeting = (typeof greeting !== 'undefined') ?  greeting : 'Welcome';

  return `${greeting} ${name}!`;
}

greet(); // Welcome Student!
greet('James'); // Welcome James!
greet('Richard', 'Howdy'); // Howdy Richard!

ES6提供了引入默认函数参数的新方法:

function greet(name = 'Student', greeting = 'Welcome') {
  return `${greeting} ${name}!`;
}

greet(); // Welcome Student!
greet('James'); // Welcome James!
greet('Richard', 'Howdy'); // Howdy Richard!

这比第一个选项更简洁。

好的方法:

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

但最好看的方法是通过以下方式查看:

if(typeof neverDeclared === typeof undefined) //also no errors and no strings

(function(){ var a= b = 3; var ed = 103; })(); //console.log(ed); //ed is not defined console.log("a defined? " + (typeof a !== 'undefined')); //no define console.log("b defined? " + (typeof b !== 'undefined')); //yes define console.log(typeof(b)); //number console.log(typeof(4+7)); //number console.log(b); //3 console.log(typeof("4"+"7")); //string var e= "ggg"; console.log(typeof(e)); //string var ty=typeof(b); console.log(ty); //number console.log(typeof false); //boolean console.log(typeof 1); //number console.log(typeof 0); //number console.log(typeof true); //boolean console.log(typeof Math.tan); //function console.log(typeof function(){}); //function if(typeof neverDeclared == "undefined") //no errors if(typeof neverDeclared === "undefined") //no errors //if(neverDeclared == null) //showing error console.log(typeof {a:1}); //object console.log(typeof null); //object console.log(typeof JSON); //object console.log(typeof Math); //object console.log(typeof /a-z/); //object console.log(typeof new Date()); //object console.log(typeof afbc); //undefined //console.log(typeof new);//error document.write("<br> * oprator as math "); var r=14*"4"; document.write(r); document.write("<br> + oprator as string "); var r=14+"44"; document.write(r); document.write("<br> Minus Operator work as mathematic "); var r=64-"44"; document.write(r); document.write("<br>"); console.log(typeof(4*"7")); //returns number console.log(typeof(4+"7")); //returns string Interview Question in JavaScript

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

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

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

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