Javascript 如何在没有hasOwnProperty的情况下检查属性

Javascript 如何在没有hasOwnProperty的情况下检查属性,javascript,Javascript,我有这样的对象var o=$scope['reservation']['bookings'][bookingKey]['founds'] 当我这样做的时候 if (o.hasOwnProperty('checkedProperty') { // code } 我有错误未捕获类型错误:无法读取未定义的属性“hasOwnProperty” 我也尝试: if (o['checkedProperty']) { // code to do if my object 'o' has 'che

我有这样的对象
var o=$scope['reservation']['bookings'][bookingKey]['founds']

当我这样做的时候

if (o.hasOwnProperty('checkedProperty') {

  // code

}
我有错误
未捕获类型错误:无法读取未定义的属性“hasOwnProperty”

我也尝试:

if (o['checkedProperty']) {

  // code to do if my object 'o' has 'checkedProperty'

}
但我有错误:
未捕获类型错误:无法读取未定义的
的属性“26”


如何检查此属性?

如果您首先没有对象,则无法检查对象上是否存在属性

检查
o
是否实际上是一个对象:

if (typeof o !== "undefined" && o.hasOwnProperty('checkedProperty')) {

o
未定义的
首先检查对象是否是
未定义的
,然后继续。
如果不想努力工作,请尝试/catch
。或者
如果(o!=null&&o.reservations!=null&&o.reservations.bookings!=null…
如果
o
null
,则此操作的可能副本也将失败。作为一般解决方案:
if(o!=null&&o.hasOwnProperty('checkedProperty'){
这假设已声明了
o
。否则:
if(o!='undefined'&&o!==null&&o.hasOwnProperty('checkedProperty'){