判断参数是否为空, 包括null, undefined, [], '', {}
obj 需判断的对象
export function isNotEmpty(obj) {
var empty = false;
if (obj === null || obj === undefined) { // null and undefined
empty = true;
} else if ((isArray(obj) || isString(obj)) && obj.length === 0) {
empty = true;
} else if (isObject(obj)) {
var hasProp = false;
for (let prop in obj) {
if (prop) {
hasProp = true;
break;
}
}
if (!hasProp) {
empty = true;
}
}
return !empty;
}
function _getClass (object) {
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1];
}
export function isObject (obj) {
return _getClass(obj).toLowerCase() === 'object';
}
优化后:
function isObject(obj){
return Object.prototype.toString.call(obj).match(/^\[object\s(.*)\]$/)[1].toLowerCase() === 'object';
}
function isNotEmpty(obj){
var empty = true;
if(obj === null || obj===undefined||obj==='null'){
empty = false;
}else if(Array.isArray(obj) && obj.length === 0){
empty = false;
}else if(isObject(obj)){
for(let prop in obj){
if(prop){
var empty = true;
}
}
}
return empty
}
网友评论