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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With