Maison  >  Questions et réponses  >  le corps du texte

问下大家是如何判断js对象为空?

这个是我的判断方法,大家还有别的判断方法吗?


phpcn_u274phpcn_u2742760 Il y a quelques jours835

répondre à tous(2)je répondrai

  • 数据分析师

    数据分析师2017-10-01 00:38:31

    Comment juger si l'objet js est vide ? - Questions et réponses sur le site Web chinois PHP - Comment jugez-vous si un objet js est vide ? - Questions et réponses sur le site Web chinois PHP

    Veuillez regarder et apprendre.

    répondre
    0
  • 阿神

    阿神2017-03-01 14:14:15

    看你对对象为空的定义
    如果判读对象是不是undefined/null
    那么直接使用if语句就可以

    var obj;
    if(!obj){
     console.log("object is null or undefined");
    }

    如果判断对象没有任何属性这个就不好弄了
    使用
    for...in能够遍历可枚举属性,包括prototype中的
    Object.keys(ES2015)值遍历自有的可枚举属性
    但是对象的属性也可以通过设置enumerable=false为不可枚举的,那么通过上面的方法你就无法判断是否具有某个属性了

    $.isEmptyObject();方法也是检查可枚举的属性

    所以具体问题还是要具体分析,根据你的业务场景来
    没有特殊的设置
    $.isEmptyObject();
    Object.keys()
    for...in
    都可以使用

    répondre
    0
  • Annulerrépondre