0

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?

lkahtz
  • 4,706
  • 8
  • 46
  • 72
  • 2
    For exact details on the `==` operator, have a look at the [ECMA 262 specification, section 11.9.3, "The Abstract Equality Comparison Algorithm"](http://es5.github.com/#x11.9.3). – Rob W Jul 11 '12 at 21:33
  • +1 Rob. §11.9.3 1 f. `Return true if x and y refer to the same object. Otherwise, return false` – RobG Jul 11 '12 at 22:59

1 Answers1

4

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614