Back to Immutable Js

ValueObject

website/docs/ValueObject.mdx

5.1.51.5 KB
Original Source

import Repl from '@/repl/Repl.tsx'; import CodeLink from '@/mdx-components/CodeLink.tsx';

ValueObject

The interface to fulfill to qualify as a Value Object.

Members

<MemberLabel label="equals" />

True if this and the other Collection have value equality, as defined by Immutable.is().

<Signature code="equals(other: unknown): boolean;" />

Note: This is equivalent to Immutable.is(this, other), but provided to allow for chained expressions.

<MemberLabel label="hashCode" /> Computes and returns the hashed identity for this Collection.

<Signature code={hashCode(): number;} />

The hashCode of a Collection is used to determine potential equality, and is used when adding this to a Set or as a key in a Map, enabling lookup via a different instance.

<Repl defaultValue={const a = List([1, 2, 3]); const b = List([1, 2, 3]); // a and b are different instances const set = Set([a]); set.has(b)} />

Note: hashCode() MUST return a Uint32 number. The easiest way to guarantee this is to return myHash | 0 from a custom implementation.

If two values have the same hashCode, they are not guaranteed to be equal. If two values have different hashCodes, they must not be equal.

Note: hashCode() is not guaranteed to always be called before equals(). Most but not all Immutable.js collections use hash codes to organize their internal data structures, while all Immutable.js collections use equality during lookups.