All objects in JavaScript come from Object . all objects inherit methods and properties from Object.prototype .
In your example, when you try to get foo.bar , it does not find bar in foo , so it is going to prototype foo and trying to find it there.
hasOwnProperty - checks only the property that is located exactly in your foo .
Here is what your foo looks like

For a deeper understanding, you can read this chapter.
You don't know js
source share