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?

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This is how it works a = 1 b = 1 a.toNumber() == b.toNumber()
Primitive types are compared by value.
@lkahtz answer updated. Primitive values are compared much differently than object references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.