为何表达式a.prototype == b.prototype的结果为true?我存在哪些理解误区?
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:
- First, it checks
aitself: Noprototypeproperty exists here (sinceais an instance, not a constructor function). - Next, it checks
a's internal prototype (A.prototype): This is a plain default object ({ constructor: A }), which also doesn't have aprototypeproperty. - Then it checks
Object.prototype(the next link in the chain): It also lacks aprototypeproperty. - Finally, it hits
null(the end of the prototype chain), so the result isundefined.
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
prototypeproperty: Functions likeAandB(your constructor functions) have aprototypeattribute that points to the shared prototype object for all instances created withnew A()ornew B(). - Instances have an internal prototype link: Instead of a
prototypeproperty, instances have an internal[[Prototype]]link (exposed in browsers as the non-standard__proto__, or accessed via the standardObject.getPrototypeOf()method). This link points to the constructor'sprototypeobject. - You were comparing the wrong values: If you wanted to check if
aandbshare the same prototype, you should compare their internal prototype links, not a non-existentprototypeproperty 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.prototypeto 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
prototypeproperty—this is exclusive to functions (and classes, which are syntactic sugar for functions).
内容的提问来源于stack exchange,提问作者Wang Liangwei

