Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Object inherits from Function.prototype which in turn inherits from Object.prototype.

this is because internally, Object is actually a function

function Object(){[native code]}

which is why we can write code like

var ob=new Object();

Object inherits properties like 'caller' , 'arity' ,etc from Function.prototype

However (and this is what's confusing)

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

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

Same way

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, someone has any idea about why this is happening ?

thank you very much. I hope I am not sounding naive

EDIT

trying Object.getOwnPropertyNames(Object) indeed returned 'caller' as a property directly on Object itself. So Object.hasOwnProperty('caller') is factually correct

But , now the question is why in MDN documentation, 'caller' is mentioned as inherited from Function. So it definitely leading to confusion .

So is this some mistake in the documentation ? thank you.

EDIT-2

Can I reach the conclusion that Object has its own

caller, length, etc properties as even Object.length and Object.__proto__.length is not the same . It should have been equal if indeed Object was inheriting length property from its [[prototype]] , i.e Function.prototype but its not the case

The thing is why does MDN mention that Object just inherits caller, length, arity , etc from its [[prototype]] object ? its a bit misleading IMHO

like image 893
Sarabjeet Avatar asked Sep 29 '22 08:09

Sarabjeet


1 Answers

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 every function has an own caller and arguments property. See ES5 Spec 15.3.5 and 13.2.

Function instances that correspond to strict mode functions (13.2) and function instances created using the Function.prototype.bind method (15.3.4.5) have properties named “caller” and “arguments” that throw a TypeError exception.

like image 163
dusky Avatar answered Oct 03 '22 01:10

dusky