[赏金编辑]
当你需要设置/使用null或者undefined需要检查它时,我正在寻找一个很好的解释.基本上这两种常见的做法是什么,并且真的可以在通用的可维护代码中单独处理它们?
我什么时候可以安全地检查=== null,安全检查=== undefined以及何时需要检查两者== null
什么时候应该使用关键字undefined,什么时候应该使用关键字null
我有各种格式的检查
if (someObj == null)或if (someObj != null)检查null和undefined.我想将所有这些改为其中之一=== undefined或者=== null我不确定如何保证它只会是两者之一而不是两者之一.
你应该在哪里使用支票null以及你应该在哪里使用支票undefined
一个具体的例子:
var List = []; // ordered list contains data at odd indexes.
var getObject = function(id) {
for (var i = 0; i < List.length; i++) {
if (List[i] == null) continue;
if (id === List[i].getId()) {
return List[i]; …Run Code Online (Sandbox Code Playgroud)