为什么我们需要 isPrototypeOf ?

发布于 2024-11-06 20:04:57 字数 631 浏览 0 评论 0原文

此页面指出:

注意: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尽揽少女心 2024-11-13 20:04:57

对象构造函数是很时髦的东西。来自这个答案

正如 Pointy 所指出的,在他的
回答

<块引用>

“构造函数”属性是
引用创建的函数
对象的原型,而不是对象本身
本身。

处理这个问题的通常方法是
扩充对象的原型
分配后的 constructor 属性
原型

对象的构造函数不是只读的,这就是为什么可以这样做。在创建 p 后,我可以为 p.constructor 分配任何值,这将完全破坏 using

o instanceof p.constructor

而不是

p.isPrototypeOf(o)

进一步阅读


编辑回复:OP编辑

此外,p.isPrototypeOf(o) 在功能上等同于 p===Object.getPrototypeOf(o) 吗?

这些比您原来的问题更相似,除了 Object.getPrototypeOf 直到 JavaScript 1.8.1 才引入这一事实?请参阅 John Resig - Object.getPrototypeOf。也许更相关的是,这两个函数在 规范中是不同的(警告,PDF链接)

15.2.3.2 Object.getPrototypeOf

15.2.4.6 Object.prototype.isPrototypeOf

Object constructors are funky things. From this answer:

As Pointy has pointed out, in his
answer

The "constructor" property is a
reference to the function that created
the object's prototype, not the object
itself.

The usual way to deal with this is to
augment the object's prototype
constructor property after assigning
to the prototype.

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 after p is created, and this would completely break using

o instanceof p.constructor

instead of

p.isPrototypeOf(o)

Further reading


Edit re: OP edit

Addtionally, is p.isPrototypeOf(o) functionally equivalent to p===Object.getPrototypeOf(o)?

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)

15.2.3.2 Object.getPrototypeOf

15.2.4.6 Object.prototype.isPrototypeOf

何止钟意 2024-11-13 20:04:57

我认为这里最重要的区别是 isPrototypeOf 方法允许您检查一个对象是否直接从另一个对象继承。请考虑以下事项:

var t = new Object();
var f = new Object();
var c = Object.create(t);

c instanceof f.constructor; // true
c instanceof t.constructor; // true
f.isPrototypeOf(c); // false
t.isPrototypeOf(c); // true

如您所见,构造函数只是实例化对象的函数。不是实现说明符。因此,如果 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:

var t = new Object();
var f = new Object();
var c = Object.create(t);

c instanceof f.constructor; // true
c instanceof t.constructor; // true
f.isPrototypeOf(c); // false
t.isPrototypeOf(c); // true

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; } and f.y = function(){ return false; } and I needed to check that c would return the appropriate implementation through it's prototype chain, instanceof wouldn't help very much.

意犹 2024-11-13 20:04:57

实例化 -->该对象(或它派生自的对象)使用命名对象作为原型

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文