js判断是否是null,undefined,NaN

mac2024-04-22  57

我们知道,如果对象是3种情况,那么就会js调用对象的属性会进行报错,那么我们在调用前应该进行判断是否为这3种情况。

//判断空或者undefined或者NAN,用它来调用方法会出错误的。 function isTransferErrorEmpty(obj){ if(Object.prototype.toString.call(obj)==="[object Null]" ){ return true; } if(Object.prototype.toString.call(obj)==="[object Undefined]" ){ return true; } if(typeof obj === 'number' && isNaN(obj)){ return true; } return false; }

 

最新回复(0)