Sar*_*eet 5 javascript inheritance
Object继承自Function.prototype,而Function.prototype继承自Object.prototype.
这是因为在内部,Object实际上是一个函数
function Object(){[native code]}
Run Code Online (Sandbox Code Playgroud)
这就是我们可以写代码的原因
var ob=new Object();
Run Code Online (Sandbox Code Playgroud)
Object继承了Function.prototype中的'caller','arity'等属性
然而(这是令人困惑的)
alert(Object.hasOwnProperty('caller')); //returns TRUE ! surprising
Run Code Online (Sandbox Code Playgroud)
不应该返回false,因为Object实际上从Function.prototype继承了'caller'属性?
同样的方式
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
Run Code Online (Sandbox Code Playgroud)
那么,有人知道为什么会这样吗?
非常感谢你.我希望我听起来不天真
编辑
尝试Object.getOwnPropertyNames(Object)确实'caller'直接在Object本身作为属性返回.所以Object.hasOwnProperty('caller')内容属实
但是,现在问题是为什么在MDN文档中,'caller'提到继承自Function.所以它肯定会导致混乱.
这是文档中的一些错误吗?谢谢.
编辑-2
我能否得出Object有自己的结论
caller,length等属性为偶数
Object.length和Object.__proto__.length不一样.如果Object确实从它继承了长度属性,那应该是相等的[[prototype]],Function.prototype
但事实并非如此
问题是,为什么MDN提到对象只是继承caller,length,arity,等从它的[[prototype]]对象?它有点误导恕我直言
来自MDN:
\n\n\n\n\n使用函数声明创建的函数是 Function 对象\n,并且具有 Function 对象的所有属性、方法和行为。
\n
在严格模式下,每个函数都有自己caller的arguments属性。请参阅 ES5 规范15.3.5和13.2。
\n\n与严格模式函数 (13.2) 和使用 Function.prototype.bind 方法创建的函数实例 (15.3.4.5) 对应的函数实例具有名为 \xe2\x80\x9ccaller\xe2\x80\x9d 和 \xe2 的属性\x80\x9carguments\xe2\x80\x9d 抛出\n TypeError 异常。
\n