'hasOwnProperty' in javascript -
i use obj.hasownproperty
judge whether object has property, when replaced obj[prop] !== undefined
, not normal implementation, ask, why behind method can not use it?
object.hasownproperty(prop); object[prop] !== undefined;
obj[prop] !== undefined
wrong 2 reasons:
- you can explicitly set property
undefined
,obj[prop] = undefined;
.obj.hasownproperty(prop)
returntrue
in case. obj[prop]
follow prototype chain, return property that's inherited.obj.hasownproperty(prop)
returnstrue
if property exists directly in object, returnsfalse
inherited properties.
Comments
Post a Comment