Wang Liangwei
Wang Liangwei

Reputation: 59

JavaScript header and prototype

Why will a.prototype == b.prototype evaluate to true? What have I misunderstood?

var a=new A();
var b=new B();
function A(){};
function B(){};

console.log (a instanceof A);
console.log(b instanceof A);
console.log(a.prototype== b.prototype);

Upvotes: 0

Views: 52

Answers (1)

Ivan
Ivan

Reputation: 40708

The instance of A and B, respectively a and b don't have a property prototype. So when you are checking a.prototype == b.prototype you're basically doing undefined == undefined (which is true).

You can check by simply logging a.prototype, it will return undefined.

But A and B do have prototypes and they are different:

var a = new A();
var b = new B();

function A() {};
function B() {};

console.log(A.prototype == B.prototype);

Upvotes: 3

Related Questions