js 中判断对象是否为空

 

var dd = function (S_object, name) {
    console.log(name + '第一步执行结果:' + S_object);
    if (typeof S_object === "object" && !(S_object instanceof Array)) {
        var hasProp = false;
        for (var prop in S_object) {
            hasProp = true;
            break;
        }
        if (hasProp) {
            S_object = [S_object];
            console.log(name + '第二步执行结果(合法对象):' + S_object);
        } else {
            //throw "this is empty object";
            console.log(name + '第三步执行结果(空对象):' + S_object);
            return false;
        }
    }
}
var t1 = {};
var t2 = [];
var t3 = null;
var t4 = {
    "data":
    "1"
};
var t5 = [{
        "data":
        "2"
    }
];
var t6 = [1, 2];
dd(t1, "t1")
dd(t2, "t2")
dd(t3, "t3")
dd(t4, "t4")
dd(t5, "t5")
dd(t6, "t6")
console.log(Array.isArray(t6));
console.log(t1 instanceof Array);
console.log(t2 instanceof Array);
console.log(t3 instanceof Array);
console.log(t4 instanceof Array);
console.log(t5 instanceof Array);
console.log(t6 instanceof Array);
console.log(String);

 

 

 

 

posted @ 2018-01-04 10:50  山顶洞外人  阅读(474)  评论(0编辑  收藏  举报