In Javascript. how can I tell if a field exists inside an object?

前端 未结 5 932
余生分开走
余生分开走 2021-01-31 07:12

And of course I want to do this code-wise. It\'s not that there isn\'t alternative to this problem I\'m facing, just curious.

5条回答
  •  半阙折子戏
    2021-01-31 07:38

    This will ignore attributes passed down through the prototype chain.

    if(obj.hasOwnProperty('field'))
    {
        // Do something
    }
    

提交回复
热议问题