How to check if anonymous object has a method?

前端 未结 6 1831
遥遥无期
遥遥无期 2020-12-23 20:15

How can I check if an anonymous object that was created as such:

var myObj = { 
    prop1: \'no\',
    prop2: function () { return false; }
}
6条回答
  •  抹茶落季
    2020-12-23 20:30

    What do you mean by an "anonymous object?" myObj is not anonymous since you've assigned an object literal to a variable. You can just test this:

    if (typeof myObj.prop2 === 'function')
    {
        // do whatever
    }
    

提交回复
热议问题