1

Here is an example.

alert(document.querySelectorAll('span').hasOwnProperty('length'));
nikhil
  • 413
  • 3
  • 9

1 Answers1

1

Probably because hasOwnProperty does not work the same cross-browser, especially since the change in April 2015 (for Chrome here...):

Check and/or update your Safari, but it still might not work since obviously they are making changes.

Perhaps you can use a different approach which works the same cross-browser such as

  • if ('prop' in obj)
  • if ('undefined' !== typeof obj['prop'])

There's slight difference in performance which shouldn't be an issue for you I think. See also this page for a reference.

See this question/answer also.

Community
  • 1
  • 1
trainoasis
  • 6,419
  • 12
  • 51
  • 82
  • Every other answer in any forum suggests that 'hasOwnProperty' is the preferred way to check if an object has a property. I guess it used to work in safari prior to 9.0.2. – nikhil Jan 07 '16 at 08:00
  • Yup, it seems it did work, but since they are obviously making changes I wouldn't force it. – trainoasis Jan 07 '16 at 10:40
  • 'prop' in obj will always return truthy value for prototype properties. typeof NodeList.length won't return undefined as expected in all browsers. – nikhil Jan 07 '16 at 18:17