Check that value is object literal?

后端 未结 12 711
逝去的感伤
逝去的感伤 2020-12-01 10:57

I have a value and want to know if it\'s an iteratable object literal, before I iterate it.

How do I do that?

12条回答
  •  醉酒成梦
    2020-12-01 11:11

    Strangely, I'm seeing different values for toString() for a subclassed object depending how toString() is being called:

    Object.prototype.toString.call(aThing)
    "[object Object]"
    
    aThing.toString()
    "ZmPopupMenu"
    

    That results in a false positive, so I amended it to prefer the object's toString():

    var str = someObject.toString
        ? someObject.toString()
        : Object.prototype.toString.call(someObject);
    return str === '[object Object]';
    

提交回复
热议问题