2

I've read this post and my question is a follow-up to it but I didn't see anyway to post a follow-up question directly, so posting a new question related to it: Why is hasOwnProperty being invoked generically here?

I see the explanation of why to call Object.prototype.hasOwnProperty(myObj) instead of myObj.hasOwnProperty - the 2nd point (from user zzzzBov) of objects being created by user supplied strings was the strongest point for me. HOWEVER. If we are going to worry about that, then why aren't we also equally worried about the other methods of Object.prototype being (accidentally?) overridden? Like isPrototypeOf? I've only seen recommendations regarding hasOwnProperty, nothing about the others, in Axel's book as well as other places.

  • 1
    *probably* because `hasOwnProperty` is one of the frequently used prototype methods. The same rule should also apply for `isPrototypeOf`. – adiga Nov 19 '19 at 18:44
  • From the top answer to the linked question: *"Often, developers will use call on a built-in type to ensure that they are getting the correct native behavior of a method and not some overridden behavior."* This statement includes all methods, not just `hasOwnProperty`. – kaya3 Nov 19 '19 at 19:54
  • Thank you, I thought the same rule, or best practice, should apply to isPrototypeOf - but none of the books and articles I've read mention any other method besides hasOwnProperty. So, what is so special about that method? Is it used so much more than isPrototypeOf? Did these books and articles really intend to imply you should do this for all of Object.prototype methods (...then why didn't they say that?... )? Is it some historical issue where hasOwnProperty existed first and frequently caused issues, so it is mentioned and not the other? @kaya3 – John Sterling Nov 19 '19 at 20:31
  • To answer *"then why didn't they say that?"*, it depends who "they" are; you would need to show us a quote from a book which only says you should do this for `hasOwnProperty`. The answer I quoted is more general and does apply to other methods. – kaya3 Nov 19 '19 at 20:34
  • Here is a link to Dr. Axel Rauschmayer's book (free online): http://speakingjs.com/es5/ch17.html#id1201176 He doesn't say not to do it for others. So, my question has evolved to: what is special about this one method? I don't believe it's coincidence that only hasOwnProperty gets discussed. I'm starting to think it's a meme - the first person discussing it used that method... now everyone is repeating it BUT the same reasoning applies to all the Object.prototype methods unless there is another rule making hasOwnProperty special. @kaya3 – John Sterling Nov 20 '19 at 16:18
  • 1
    The reason `hasOwnProperty` gets singled out when mentioned this way might be psychological. The purpose of this method is to test whether a property belongs to an object or its prototype chain, so naturally one might wonder whether `hasOwnProperty` itself belongs to the object or the prototype chain. As soon as you wonder that, it is easy to deduce that if it does belong to the object itself, then it presumably won't do what you want. – kaya3 Nov 20 '19 at 16:24

0 Answers0