js中typeof的用法汇总
示例:if(typeof(this.is_count[i])!='undefined'){}
JavaScript中的typeof
其实非常复杂,它可以用来做很多事情,但同时也有很多怪异的表现.本文列举出了它的多个用法,而且还指出了存在的问题以及解决办法.
> typeof undefined
'undefined'
> typeof null // well-known bug
'object'
> typeof true
'boolean'
> typeof 123
'number'
> typeof "abc"
'string'
> typeof function() {}
'function'
> typeof {}
'object'
> typeof []
'object'
> typeof unknownVariable
'undefined'st
1.检查一个变量是否存在,是否有值typeof
在两种情况下会返回"undefined":一个变量没有被声明的时候,和一个变量的值是undefined的时候.例如:
> typeof undeclaredVariable === "undefined" true
> var declaredVariable;
> typeof declaredVariable 'undefined'
> typeof undefined 'undefined'
还有其他办法检测某个值是否是undefined:
> var value = undefined; > value === undefined true`
但这种方法如果使用在一个未声明的变量上的时候,就会抛出异常,因为只有typeof才可以正常检测未声明的变量的同时还不报错:
> undeclaredVariable === undefined ReferenceError: undeclaredVariable is not defined
注意:未初始化的变量,没有被传入参数的形参,不存在的属性,都不会出现上面的问题,因为它们总是可访问的,值总是undefined:
> var declaredVariable; > declaredVariable === undefined true
> (function (x) { return x === undefined }()) true > ({}).foo === undefined true
译者注:因此,如果想检测一个可能没有被声明的全局变量是否存在,也可以使用 if(window.maybeUndeclaredVariable){}
问题: typeof在完成这样的任务时显得很繁杂.
解决办法: 这样的操作不是很常见,所以有人觉的没必要再找更好的解决办法了.不过也许有人会提出一个专门的操作符:
> defined undeclaredVariable false > var declaredVariable; > defined declaredVariable false
或者,也许有人还需要一个检测变量是否被声明的操作符:
> declared undeclaredVariable false > var declaredVariable; > declared declaredVariable true
译者注:在perl里,上面的defined操作符相当于defined()
,上面的declared操作符相当于exists()
,
2.判断一个值不等于undefined也不等于null问题:如果你想检测一个值是否被定义过(值不是undefined也不是null),那么你就遇到了typeof最有名的一个怪异表现(被认为是一个bug):typeof null返回了"object":
> typeof null 'object'
译者注:这只能说是最初的JavaScript实现的bug,而现在标准就是这样规范的.V8曾经修正并实现过 typeof null === "null",但最终证明不可行 解决办法: 不要使用typeof来做这项任务,用下面这样的函数来代替:
function isDefined(x) {
return x !== null && x !== undefined;
}
另一个可能性是引入一个“默认值运算符”,在myValue未定义的情况下,下面的表达式会返回defaultValue:myValue ?? defaultValue 上面的表达式等价于: (myValue !== undefined && myValue !== null) ? myValue : defaultValue 又或者: myValue ??= defaultValue 其实是下面这条语句的简化: myValue = myValue ?? defaultValue 当你访问一个嵌套的属性时,比如bar,你或许会需要这个运算符的帮助: