- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么在 JavaScript 中,Object instanceof Function
和 Function instanceof Object
都返回 true
?
我在 Safari WebInspector 中尝试过。
最佳答案
我花了一段时间才弄清楚,但确实值得花时间。首先,让我们看看如何instanceof
有效。
引用自MDN ,
The
instanceof
operator tests whether an object has in its prototype chain theprototype
property of a constructor.
[instanceof]
现在,让我们看看如何 instanceof
由 ECMA 5.1 规范定义,
The production
RelationalExpression: RelationalExpression instanceof ShiftExpression
is evaluated as follows:
- Let
lref
be the result of evaluatingRelationalExpression
.- Let
lval
beGetValue(lref)
.- Let
rref
be the result of evaluatingShiftExpression
.- Let
rval
beGetValue(rref)
.- If
Type(rval)
is not Object, throw aTypeError
exception.- If
rval
does not have a[[HasInstance]]
internal method, throw aTypeError
exception.- Return the result of calling the
[[HasInstance]]
internal method ofrval
with argumentlval
.
首先计算左侧和右侧表达式 ( GetValue
),然后右侧结果应该是带有 [[HasInstance]]
的对象内部方法。并非所有对象都会有 [[HasInstance]]
内部方法,但是函数。例如,以下将失败
console.log(Object instanceof {});
# TypeError: Expecting a function in instanceof check, but got #<Object>
[[HasInstance]]
现在,让我们看看如何 [[HasInstance]]
已在 ECMA 5.1 规范中定义,
Assume
F
is a Function object.When the
[[HasInstance]]
internal method ofF
is called with valueV
, the following steps are taken:
- If
V
is not an object, returnfalse
.- Let
O
be the result of calling the[[Get]]
internal method ofF
with property name"prototype"
.- If
Type(O)
is not Object, throw aTypeError
exception.- Repeat
- Let
V
be the value of the[[Prototype]]
internal property ofV
.- If
V
isnull
, returnfalse
.- If
O
andV
refer to the same object, returntrue
.
就这么简单。采取prototype
F
的属性(property)并将其与 [[Prototype]]
进行比较O
的内部属性直到变成null
或prototype
的F
与 O
相同.
[[prototype]]
内部属性首先让我们看看什么是 [[prototype]]
internal property ,
All objects have an internal property called
[[Prototype]]
. The value of this property is eithernull
or an object and is used for implementing inheritance. Whether or not a native object can have a host object as its[[Prototype]]
depends on the implementation. Every[[Prototype]]
chain must have finite length (that is, starting from any object, recursively accessing the[[Prototype]]
internal property must eventually lead to anull
value).
注意:我们可以通过 Object.getPrototypeOf
获取这个内部属性。功能。
prototype
属性 [[HasInstance]]
还谈到另一个名为 prototype
的属性,特定于 Function
对象。
The value of the
prototype
property is used to initialise the[[Prototype]]
internal property of a newly created object before the Function object is invoked as a constructor for that newly created object.
这意味着,当函数对象用作构造函数时,将创建一个新对象,并且新对象将具有其内部 [[Prototype]]
用这个 prototype
初始化属性(property)。例如,
function Test() {}
Test.prototype.print = console.log;
console.log(Object.getPrototypeOf(new Test()) === Test.prototype);
# true
现在让我们回到实际问题。我们来看第一个案例
console.log(Object instanceof Function);
# true
它将获取Function.prototype
首先,它会尝试查找该对象是否在 Object
的原型(prototype)层次结构中。让我们看看结果如何
console.log(Function.prototype);
# [Function: Empty]
console.log(Object.getPrototypeOf(Object));
# [Function: Empty]
console.log(Object.getPrototypeOf(Object) === Function.prototype);
# true
自 Function.prototype
匹配Object
的内部属性 [[Prototype]]
,它返回true
.
现在我们来看第二种情况
console.log(Function instanceof Object);
# true
console.log(Object.prototype);
# {}
console.log(Object.getPrototypeOf(Function));
# [Function: Empty]
console.log(Object.getPrototypeOf(Function) === Object.prototype);
# false
console.log(Object.getPrototypeOf(Object.getPrototypeOf(Function)));
# {}
Object.getPrototypeOf(Object.getPrototypeOf(Function)) === Object.prototype
# true
在这里,首先我们得到Object.prototype
,即{}
。现在它正在尝试查找是否有相同的对象 {}
Function
中有吗?的原型(prototype)链。 Function
的直接父级is 和 Empty 函数。
console.log(Object.getPrototypeOf(Function));
# [Function: Empty]
它与 Object.prototype
不同。
console.log(Object.getPrototypeOf(Function) === Object.prototype);
# false
但是[[HasInstance]]
算法并不止于此。它重复并上升一级
console.log(Object.getPrototypeOf(Object.getPrototypeOf(Function)));
# {}
这与 Object.prototype
相同。这就是为什么返回 true
.
关于javascript - 为什么在 JavaScript 中 "Object instanceof Function"和 "Function instanceof Object"都返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42465023/
我遵循了一本名为“Sitepoint Full Stack Javascript with MEAN”的书中的教程,我刚刚完成了第 6 章,应该已经创建了一个带有“数据库”的“服务器”。数据库只不过是
在 Jquery 中,我创建两个数组,一个嵌入另一个数组,就像这样...... arrayOne = [{name:'a',value:1}, {name:'b',value:2}] var arra
这个问题在这里已经有了答案: What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wa
我被放在别人的代码上,有一个类用作其他组件的基础。当我尝试 ng serve --aot(或 build --prod)时,我得到以下信息。 @Component({ ...,
我正在测试一些代码,并使用数据创建了一个 json 文件。 问题是我在警报中收到“[object Object],[object Object]”。没有数据。 我做错了什么? 这是代码:
我想打印 [object Object],[object Object] 以明智地 "[[{ 'x': '1', 'y': '0' }, { 'x': '2', 'y': '1' }]]"; 在 ja
我有一个功能 View ,我正在尝试以特殊格式的方式输出。但我无法让列表功能正常工作。 我得到的唯一返回是[object Object][object Object] [object Object]
在使用优秀的 Sim.js 和 Three.js 库处理 WebGL 项目时,我偶然发现了下一个问题: 一路走来,它使用了 THREE.Ray 的下一个构造函数: var ray = new THRE
我正在使用 Material UI 进行多重选择。这是我的代码。 {listStates.map(col => (
我的代码使用ajax: $("#keyword").keyup(function() { var keyword = $("#keyword").val(); if (keyword.
我遇到了下一个错误,无法理解如何解决它。 Can't resolve all parameters for AuthenticationService: ([object Object], ?, [o
我正在尝试创建一个显示动态复选框的表单,至少应选中其中一个才能继续。我还需要获取一组选中的复选框。 这是组件的代码: import { Component, OnInit } from '@angul
我正在开发 NodeJs 应用程序,它是博客应用程序。我使用了快速验证器,我尝试在 UI 端使用快速闪存消息将帖子保存在数据库中之前使用闪存消息验证数据,我成功地将数据保存在数据库中,但在提交表单后消
我知道有些人问了同样的问题并得到了解答。我已经查看了所有这些,但仍然无法解决我的问题。我有一个 jquery snipet,它将值发送到处理程序,处理程序处理来自 JS 的值并将数据作为 JSON 数
我继承了一个非常草率的项目,我的任务是解释为什么它不好。我注意到他们在整个代码中都进行了这样的比较 (IQueryable).FirstOrDefault(x => x.Facility == fac
我只是在删除数组中的对象时偶然发现了这一点。 代码如下: friends = []; friends.push( { a: 'Nexus', b: 'Muffi
这两个代码片段有什么区别: object = nil; [object release] 对比 [object release]; object = nil; 哪个是最佳实践? 最佳答案 object
我应该为其他人将从中继承的第一个父对象传递哪个参数,哪个参数更有效 Object.create(Object.prototype) Object.create(Object) Object.creat
我在不同的对象上安排不同的选择器 [self performSelector:@selector(doSmth) withObject:objectA afterDelay:1]; [self per
NSLog(@"%p", &object); 和 NSLog(@"%p", object); 有什么区别? 两者似乎都打印出一个内存地址,但我不确定哪个是对象的实际内存地址。 最佳答案 这就是我喜欢的
我是一名优秀的程序员,十分优秀!