Javascript :: Why does Object.hasOwnProperty ('caller') return true?

An object inherits from Function.prototype, which in turn inherits from Object.prototype.

this is because inside, Object is actually a function

function Object(){[native code]} 

so we can write code like

 var ob=new Object(); 

An object inherits properties such as "caller", "arity", etc. from Function.prototype

However (and this is what confuses)

 alert(Object.hasOwnProperty('caller')); //returns TRUE ! surprising 

shouldn't it return false, since Object actually inherits the caller property from Function.prototype?

Same

 alert(Function.hasOwnProperty('caller')); /*returns True. expected 'false' as Function object has no property of its own and inherits everything from Function.prototype*/ alert(Number.hasOwnProperty('caller')); //again returns true, unexpectedly 

So, does anyone have an idea of ​​why this is happening?

Thank you very much. I hope I'm not naive.

EDIT

try Object.getOwnPropertyNames(Object) really returned 'caller' as a property directly directly in Object. So Object.hasOwnProperty('caller') is actually correct

But now the question is why in the MDN documentation 'caller' is referred to as inherited from Function. Thus, this definitely leads to confusion.

So what is the error in the documentation? thanks.

EDIT-2

Can I conclude that Object has its own

caller , length etc. properties like even Object.length and Object.__proto__.length do not match. It should have been equal if Object actually inherited the length property from its [[prototype]] , ie Function.prototype but not in this case

Why does MDN mention that Object only inherits caller , length , arity , etc. from its object [[prototype]] ? its a little misleading IMHO

+5
source share
1 answer

From MDN :

A function created with a function declaration is a Function object and has all the properties, methods, and behavior of Function objects.

In strict mode, each function has its own caller and arguments property. See ES5 Spec 15.3.5 and 13.2 .

Functional instances corresponding to strict modes (13.2) and functions created using the Function.prototype.bind method (15.3.4.5) have properties with the name "caller" and "argument" that throw an exception of type Error.

+4
source

Source: https://habr.com/ru/post/1207715/


All Articles