is()

Object.is类似的相等比较方法,但其是比较两个Collection是否有相同的值。

is(first: any, second: any): boolean

用于比较两个不可变数据是否相等,包括Map的键值对和Set的成员。

import { Map, is } from 'immutable'
const map1 = Map({ a: 1, b: 1, c: 1 })
const map2 = Map({ a: 1, b: 1, c: 1 })
assert(map1 !== map2)
assert(Object.is(map1, map2) === false)
assert(is(map1, map2) === true)
// 译者注:assert为断言,传入值为false时将报错。Console.assert

is()不仅仅能比较原始的字符串、数值和不可变集合比如MapSet,也能比较实现了包含equals()hashCode()两个方法的ValueObject

注意:和Object.is不同的是,Immutable.is假定0-0是相同的,与ES6的Map键值相匹配。

results matching ""

    No results matching ""