You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何表达式a.prototype == b.prototype的结果为true?我存在哪些理解误区?

Why does a.prototype == b.prototype return true in my JavaScript code?

Let's break this down step by step to clear up the confusion—this is a super common pitfall when learning JavaScript prototypes!

First, let's revisit your code for context:

var a=new A(); var b=new B(); function A(){}; function B(){}; 
console.log (a instanceof A); // true
console.log(b instanceof A); // false
console.log(a.prototype== b.prototype); // true (the confusing result)

The Core Issue: Instances Don't Have a prototype Property

The key mistake here is assuming that instance objects (like a and b) have a prototype attribute—they don't.

When you try to access a.prototype, JavaScript traverses the prototype chain to look for this property:

  1. First, it checks a itself: No prototype property exists here (since a is an instance, not a constructor function).
  2. Next, it checks a's internal prototype (A.prototype): This is a plain default object ({ constructor: A }), which also doesn't have a prototype property.
  3. Then it checks Object.prototype (the next link in the chain): It also lacks a prototype property.
  4. Finally, it hits null (the end of the prototype chain), so the result is undefined.

The exact same logic applies to b.prototype—it also evaluates to undefined. Since undefined == undefined is true, that's why you're seeing that unexpected result.

Your Key Misunderstandings About Prototypes

Let's clarify the critical distinctions you might be mixing up:

  • Constructor functions have a prototype property: Functions like A and B (your constructor functions) have a prototype attribute that points to the shared prototype object for all instances created with new A() or new B().
  • Instances have an internal prototype link: Instead of a prototype property, instances have an internal [[Prototype]] link (exposed in browsers as the non-standard __proto__, or accessed via the standard Object.getPrototypeOf() method). This link points to the constructor's prototype object.
  • You were comparing the wrong values: If you wanted to check if a and b share the same prototype, you should compare their internal prototype links, not a non-existent prototype property on the instances.

The Correct Way to Compare Instance Prototypes

Modify your code to check the actual prototype links, and you'll get the result you probably expected:

console.log(Object.getPrototypeOf(a) === Object.getPrototypeOf(b)); // false
// Or using the non-standard __proto__ (not recommended for production):
// console.log(a.__proto__ === b.__proto__); // false

This returns false because a's prototype is A.prototype and b's prototype is B.prototype—two distinct objects.

Quick Recap to Solidify Your Understanding

  • Use Constructor.prototype to access the shared prototype object for all instances of that constructor.
  • Use Object.getPrototypeOf(instance) to access the prototype object an instance inherits from.
  • Instances never have their own prototype property—this is exclusive to functions (and classes, which are syntactic sugar for functions).

内容的提问来源于stack exchange,提问作者Wang Liangwei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:07:58