lkahtz
lkahtz

Reputation: 4796

equivalence in javascript between constructor function and object

I understand the construction function is a special function returning a object. But

> Animal = function (){this.species='animal'}
> a=new Animal()
> b={species:'animal'}
> a==b

==> false

Why?

Upvotes: 0

Views: 54

Answers (1)

Pointy
Pointy

Reputation: 413757

Comparisons like that are not "deep" comparisons. Either "a" and "b" refer to the exact same object or they don't.

To put it another way, by comparing the two variables you're comparing references to objects, not the objects themselves.

edit — there's a difference between primitive types (booleans, numbers, strings) and object references. Like I said, what you've got in your question is a pair of object references. Two object references are considered equal if they refer to the same object. In your case, they don't. They're two different objects that happen to have the same property with the same value. The properties of the objects do not play a part in the == comparison because that's simply how the language is defined to work.

Upvotes: 4

Related Questions