JS判断是否空值

判断是否空值

console.log(isNull("false"))  // false 字符串不为空
console.log(isNull([]))  // true 数组为空
function isNull(obj) {
    // 字符串和数组判空
    if (obj && obj.length > 0) {
        return false;
    }
    // 按照对应的数据类型进行数据判空
    let objType = Object.prototype.toString.call(obj);
    // 字符串和数组判空
    if (objType === '[object Array]' || objType === '[object String]') {
        if (obj && obj.length > 0) {
            return false;
        }
    }
    // 如果是对象
    if (objType === '[object Object]' && !(JSON.stringify(obj) == "{}")) {
        return false;
    }
    return true;
}

function isNotNull(obj){
    return !this.isNull(obj);
}

已有 0 条评论

    欢迎您,新朋友,感谢参与互动!