简体   繁体   English

Javascript ::为什么Object.hasOwnProperty('caller')返回true?

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

Object inherits from Function.prototype which in turn inherits from Object.prototype. 对象从Function.prototype继承,而Function.prototype又从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 对象从Function.prototype继承'caller','arity'等属性

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 ? 它不应该返回false,因为Object实际上是从Function.prototype继承了“ caller”属性?

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. 尝试Object.getOwnPropertyNames(Object)确实直接在Object本身上作为属性返回了'caller' So Object.hasOwnProperty('caller') is factually correct 所以Object.hasOwnProperty('caller')实际上是正确的

But , now the question is why in MDN documentation, 'caller' is mentioned as inherited from Function. 但是,现在的问题是,为什么在MDN文档中提到'caller'是从Function继承的。 So it definitely leading to confusion . 因此肯定会引起混乱。

So is this some mistake in the documentation ? 那么这是文档中的一些错误吗? thank you. 谢谢。

EDIT-2 编辑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 . 甚至Object.lengthObject.__proto__.length callerlength等属性都不相同。 It should have been equal if indeed Object was inheriting length property from its [[prototype]] , ie Function.prototype but its not the case 如果Object实际上是从其[[prototype]]继承了length属性,即Function.prototype ,则应该相等,但事实并非如此

The thing is why does MDN mention that Object just inherits caller , length , arity , etc from its [[prototype]] object ? 问题是为什么MDN提到Object只是从其[[prototype]]对象继承了callerlengtharity等? its a bit misleading IMHO 它有点误导恕我直言

From MDN : MDN

A function created with a function declaration is a Function object and has all the properties, methods and behavior of Function objects. 使用函数声明创建的函数是Function对象,并具有Function对象的所有属性,方法和行为。

In strict mode every function has an own caller and arguments property. 在严格模式下,每个函数都有自己的callerarguments属性。 See ES5 Spec 15.3.5 and 13.2 . 参见ES5规范15.3.513.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. 对应于严格模式函数(13.2)的函数实例和使用Function.prototype.bind方法创建的函数实例(15.3.4.5)具有名为“调用者”和“参数”的属性,这些属性引发TypeError异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM