Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript::为什么Object.hasOwnProperty(';调用者';)返回true?_Javascript_Inheritance - Fatal编程技术网

Javascript::为什么Object.hasOwnProperty(';调用者';)返回true?

Javascript::为什么Object.hasOwnProperty(';调用者';)返回true?,javascript,inheritance,Javascript,Inheritance,对象继承自Function.prototype,而Function.prototype又继承自Object.prototype 这是因为在内部,对象实际上是一个函数 function Object(){[native code]} 这就是为什么我们可以像这样编写代码 var ob=new Object(); 对象从Function.prototype继承诸如“caller”、“arity”等属性 然而(这是令人困惑的) 既然对象实际上从Function.prototype继承了“caller

对象继承自Function.prototype,而Function.prototype又继承自Object.prototype

这是因为在内部,对象实际上是一个函数

function Object(){[native code]}
这就是为什么我们可以像这样编写代码

var ob=new Object();
对象从Function.prototype继承诸如“caller”、“arity”等属性

然而(这是令人困惑的)

既然对象实际上从Function.prototype继承了“caller”属性,它不应该返回false吗

同样的方式

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
有人知道为什么会这样吗

多谢各位。我希望我听起来不幼稚

编辑

尝试
Object.getOwnPropertyNames(Object)
确实返回了
“调用者”
,作为对象本身的属性。 因此
Object.hasOwnProperty('caller')
实际上是正确的

但是,现在的问题是,为什么在MDN文档中,
'caller'
被称为继承自函数。 所以这肯定会导致混乱

那么这是文档中的错误吗? 多谢各位

编辑-2

我能得出这样的结论:物体有它自己的存在吗

调用方
长度
等属性 平平
Object.length
Object.\uuuu proto\uuuu.length
不一样。如果对象确实从其
[[prototype]]
继承了长度属性,即
函数。prototype
但事实并非如此

问题是为什么MDN提到对象只是从其
[[prototype]]
对象继承
调用者
长度
算术等?这有点误导我,因为:

使用函数声明创建的函数是函数对象 并具有函数对象的所有属性、方法和行为

在严格模式下,每个函数都有自己的
调用方
参数
属性。参见ES5规范和标准

对应于严格模式函数(13.2)和 使用function.prototype.bind方法创建的函数实例 (15.3.4.5)具有名为“调用者”和“参数”的属性,这些属性会引发 TypeError异常


呃,
对象
可能不是使用函数声明创建的函数?但我没有使用严格模式,因为我没有使用
使用严格模式函数
对象本身也没有任何自己的属性。所有这些都继承自Function.prototype。所以即使对象是一个函数,它也不应该显示
Object.hasOwnProperty('caller')作为true@Sarabjeet您无法控制
对象
的创建。显然,它们是在严格的模式下创建的。请阅读。
调用者
参数
属性将在创建时添加,而不是继承。浏览器/JS引擎在哪个浏览器中执行
对象。hasOwnProperty(“调用者”)
产生
true
?我无法在Opera中复制。@例如,在当前Firefox中的Bergi
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