如何判断一个对象是否是空对象

a.方式1

    let obj1 = {
        name: "lwj",
        say: function() {
            console.log("abc");
        }
    };
    let obj2 = {};
   
    let str1 = JSON.stringify(obj1);
    let str2 = JSON.stringify(obj2);
    console.log(str1);
    console.log(str2);
    console.log(str2 === "{}");

如何判断一个对象是否是空对象

 

 


b.方式2

    let obj1 = {
        name: "lwj",
        say: function() {
            console.log("abc");
        }
    };
    let obj2 = {};
    // getOwnPropertyNames 获取指定对象所有的属性
    let keys1 = Object.getOwnPropertyNames(obj1);
    let keys2 = Object.getOwnPropertyNames(obj2);
    console.log(keys1);
    console.log(keys2);
    console.log(keys2.length === 0);

如何判断一个对象是否是空对象

 

 


c.方式3

    let obj1 = {
        name: "lwj",
        say: function() {
            console.log("abc");
        }
    };
    let obj2 = {};

    let keys1 = Object.keys(obj1);
    let keys2 = Object.keys(obj2);
    console.log(keys1);
    console.log(keys2);
    console.log(keys2.length === 0);

如何判断一个对象是否是空对象

 


 

上一篇:拷贝


下一篇:基于Matlab脚本实现对串口数据的实时采集绘图