[英]why **(Object.__proto__ instanceof Function)** === false?
why does Object._ proto _ instanceof Function gives me false? 为什么Object._ proto _ instanceof函数给我假?
alert(Object.__proto__ ); // clearly Object.__proto__ is a function right?
alert(typeof Object.__proto__); // clearly Object.__proto__ is a function right?
alert(Object.__proto__ instanceof Function); // !
Not all functions are created via the Function
constructor. 并非所有函数都是通过
Function
构造函数创建的。 instanceof
checks specifically to see if the given item was created by that specific function . instanceof
专门检查给定项是否是由该特定函数创建的。
You get a similar effect in browser environments when dealing with multiple windows. 在处理多个窗口时,您在浏览器环境中会获得类似的效果。 I mean, if you have function
foo
in window A: 我的意思是,如果你在窗口A中有函数
foo
:
function foo(arg) {
if (arg instanceof Array) {
// It's an array, do something
}
}
...and you have code in another window B that calls it: ...并且您在另一个调用它的窗口 B中有代码:
opener.foo([]);
...then you'd expect foo
to realize that arg
is an array, right? ...然后你会期望
foo
意识到arg
是一个数组,对吗? But it doesn't, because although arg
is an array, it wasn't created by the Array
constructor in the window that foo
is in. 但事实并非如此,因为虽然
arg
是一个数组,但它并不是由foo
所在窗口中的Array
构造函数创建的。
More about figuring out what things are here: Say what? 更多关于弄清楚这里有什么东西: 说什么?
If you're fascinated by this stuff (as you seem to be), there's nothing quite like reading the specification . 如果你对这些东西很着迷(就像你看起来那样),那就没有什么比阅读规范的了 。 Yes, the prose is...dry...and the terminology is....dense...but it gets more and more interesting as you learn more and more about the underlying workings.
是的,散文是......干......而且术语......密集......但随着你越来越多地了解基础工作,它会变得越来越有趣。
Off-topic : Beware that __proto__
is non-standard and not supported by all JavaScript implementations. __proto__
主题 :请注意__proto__
是非标准的,并且不受所有JavaScript实现的支持。
To end with the mystery: 结束这个谜:
What exactly is Object.__proto__
? Object.__proto__
究竟是什么?
It is simply a reference to the Function.prototype
object. 它只是对
Function.prototype
对象的引用。
Object.__proto__ === Function.prototype; // true
The Object
constructor as almost all of the built-in and user defined functions, inherit from Function.prototype
. Object
构造函数几乎是所有内置函数和用户定义函数,都继承自Function.prototype
。
This object ( Function.prototype
) is described in the specification to be a Function object , but obviously, an object can't inherit from itself , and that's why it inherits from Object.protoype
instead. 这个对象(
Function.prototype
) 在规范中被描述为一个Function对象 ,但显然, 一个对象不能从它自己继承 ,这就是它继承Object.protoype
的原因。
Your test: 你的考试:
Object.__proto__ instanceof Function; // false, which is equivalent to:
Function.prototype instanceof Function; // false
Is simply telling us that the Function.prototype
object is not on the prototype chain of Function.prototype
itself: 只是告诉我们
Function.prototype
对象不在 Function.prototype
本身的原型链上:
Function.prototype.isPrototypeOf(Function.prototype); // false, equivalent to:
Object.prototype.isPrototypeOf.call(Function.prototype, Function.prototype);
As I told before, in the specification this object described with the following characteristics, if you are interested: 正如我之前所说,如果您感兴趣, 在说明书中此对象具有以下特征:
It is a function object (implements the [[Call]]
internal method). 它是一个函数对象(实现
[[Call]]
内部方法)。
typeof Function.prototype; // "function"
The value of its [[Class]]
internal property is "Function"
. 其
[[Class]]
内部属性的值为"Function"
。
Object.prototype.toString.call(Function.prototype); // "[object Function]"
The value of its [[Prototype]]
internal property points to Object.prototype
(as you know now). 其
[[Prototype]]
内部属性的值指向Object.prototype
(如您所知)。
Object.prototype.isPrototypeOf(Function.prototype); // true
Can be called with any number of arguments. 可以使用任意数量的参数调用。
It always returns the undefined
value. 它总是返回
undefined
值。
Its length
property is 0
. 它的
length
属性是0
。
The initial value of its [[Extensible]]
internal property is true
. 其
[[Extensible]]
内部属性的初始值为true
。
alert(Object.__proto__ ); // clearly Object.__proto__ is a function right?
alert(typeof Object.__proto__); // clearly Object.__proto__ is a function right?
alert(Object.__proto__ instanceof Function); // !
Exactly, it is a function. 确切地说,它是一个功能。 However, it is not a function object .
但是,它不是一个功能对象 。 That's why instanceof returns false.
这就是为什么instanceof返回false。 You can read all about instanceof here .
你可以在这里阅读关于instanceof的所有内容。
the image in this post will help you understand the Function vs Object relation 本文中的图像将帮助您理解函数与对象的关系
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.