0
votes

Why does:

console.log(Object.hasOwnProperty("hasOwnProperty"));   

return a false, But:

console.log(Object.prototype.hasOwnProperty("hasOwnProperty"));  

return a true statement?

I realize that hasOwnProperty is a pre-built method in the Object but I guess my question is what is the difference between the Object and the prototype of an Object.
Aren't they one and the same?
When we refer to the Object in the first line of code, aren't we referring to the same line of code in the second line of code above?

EDIT: fixed above 2 lines of code from:

console.log(Object.hasOwnProperty);

and:

console.log(Object.prototype.hasOwnProperty);  
2
Since hasOwnProperty is a function, I don't see how you can be getting true or false on the console... can you provide some complete code that demonstrates what you're seeing? - Dancrumb
Umm, neither of them should log a Boolean, but the same function? - Bergi
FWIW: Object.prototype.hasOwnProperty === Object.hasOwnProperty is true - Dancrumb
I aplogize my code should have read: console.log(Object.hasOwnProperty("hasOwnProperty")); console.log(Object.prototype.hasOwnProperty("hasOwnProperty")); - Moolla

2 Answers

1
votes

Both Object.hasOwnProperty and Object.prototype.hasOwnProperty reference the same function. Object.prototype contains that function as an own property, while Object contains it as an inherited property.

So, in other words, the hasOwnProperty function is defined (as a method) on the Object.prototype object. Then, the Object constructor (like almost all other native objects) inherits (all methods) from Object.prototype.

Btw, the inheritance chain (prototype chain) of Object is:

Object -> Function.prototype -> Object.prototype -> null

So, Object inherits all methods from both Function.prototype, and Object.prototype.

0
votes

I realize that hasOwnProperty is a pre-built method in the Object.

Actually its a function on a non-enumerable property of the object from which all JavaScript objects inherit. This includes the Object function object.

but I guess my question is what is the difference between the Object and the prototype of an Object. Aren't they one and the same?

No, they're definitely not. The section on Objects in the Language Overview of the EcmaScript specification describes it pretty well I think. Each object has a hidden [[prototype]] link to the object it inherits from (until null) - building the "prototype chain" in which properties are looked up. The public prototype property of functions is different from that - it points to the object from which all instances that are constructed by the function will inherit.