为什么我们需要 isPrototypeOf ?
此页面指出:
注意:isPrototypeOf 不同于 实例操作符。在表达式中 AFunction 的对象实例, 检查对象原型链 针对 AFunction.prototype,而不是 针对 AFunction 本身
好吧,我真的不明白他们想告诉我们什么。 object instanceof AFunction
不是与 `AFunction.prototype.isPrototypeOf(object) 完全相同吗?还是我错了?
为什么我们需要 isPrototypeOf
?
如果我需要做 p.isPrototypeOf(o)
我不能只做 o instanceof p.constructor
吗?
另外,p.isPrototypeOf(o)
在功能上等同于 p===Object.getPrototypeOf(o)
吗?
this page states:
Note: isPrototypeOf differs from
instanceof operator. In the expression
object instanceof AFunction, the
object prototype chain is checked
against AFunction.prototype, not
against AFunction itself
Ok I don't really get what they are trying to tell us. Isn't object instanceof AFunction
exactly the same as `AFunction.prototype.isPrototypeOf(object)? or am I wrong?
Why do we need the isPrototypeOf
at all?
If i ever need to do p.isPrototypeOf(o)
couldn't I just do o instanceof p.constructor
?
Addtionally, is p.isPrototypeOf(o)
functionally equivalent to p===Object.getPrototypeOf(o)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对象构造函数是很时髦的东西。来自这个答案:
对象的构造函数不是只读的,这就是为什么可以这样做。在创建
p
后,我可以为p.constructor
分配任何值,这将完全破坏 using而不是
进一步阅读
构造函数
@ MDC编辑回复:OP编辑
这些比您原来的问题更相似,除了
Object.getPrototypeOf
直到 JavaScript 1.8.1 才引入这一事实?请参阅 John Resig -Object.getPrototypeOf
。也许更相关的是,这两个函数在 规范中是不同的! (警告,PDF链接)Object constructors are funky things. From this answer:
An object's constructor is not read-only, which is why this is possible to do at all. I could assign any value to
p.constructor
afterp
is created, and this would completely break usinginstead of
Further reading
constructor
@ MDCEdit re: OP edit
Those are more similar than your original question, aside from the fact that
Object.getPrototypeOf
wasn't introduced until JavaScript 1.8.1? See John Resig -Object.getPrototypeOf
. Perhaps more relevant, the two functions are different in the spec! (warning, PDF link)我认为这里最重要的区别是 isPrototypeOf 方法允许您检查一个对象是否直接从另一个对象继承。请考虑以下事项:
如您所见,构造函数只是实例化对象的函数。不是实现说明符。因此,如果 ty = function(){ return true; } 和
fy = function(){ return false; }
并且我需要检查c
是否会通过其原型链返回适当的实现,instanceof
没有太大帮助。I think the most important distinction here is that the isPrototypeOf method allows you to check if an object inherits directly from another object. Consider the following:
As you can see the constructor is only the function that instantiated the object. Not the implementation specifier. So if
t.y = function(){ return true; }
andf.y = function(){ return false; }
and I needed to check thatc
would return the appropriate implementation through it's prototype chain,instanceof
wouldn't help very much.实例化 -->该对象(或它派生自的对象)使用命名对象作为原型
isPrototypeOf -->该对象被命名对象(或其派生对象)用作原型。
IE。
instanceOf 正在查询对象的祖先。
IsPrototypeOf 正在查询对象后代。
instanceOf --> This object (or the objects it was derived from ) used the named object as a prototype
isPrototypeOf --> This object was used by the named Object (or the Objects it was derived from) as a prototype.
ie.
instanceOf is querying the objects ancestors.
IsPrototypeOf is querying the objects descendants.