Benefit of using Object.hasOwnProperty vs. testing if a property is undefined

后端 未结 3 1946
心在旅途
心在旅途 2021-02-05 11:02

Since hasOwnProperty has some caveats and quirks (window / extensive use in Internet Explorer 8 issues, etc.):

Is there any reason to even use it

3条回答
  •  感情败类
    2021-02-05 11:24

    hasOwnProperty does not check for undefined values. It only checks if a property is assigned to the object even if is undefined:

    var obj = { a : undefined };
    obj.hasOwnProperty("a") // true
    obj.a === undefined     // true
    obj.hasOwnProperty("b") // false
    obj.b === undefined     // true
    

提交回复
热议问题