精慕HU
现在在大多数地方都受支持,并且对于运行时类型检查特别有用。我分享这篇文章是为了了解如何transform使用这种现代功能来表达 -function transform (f, o){ switch (o?.constructor) // <- any o, even null and undefined { case Array: return o.map(_ => transform(f, _)) case Object: return Object.fromEntries ( Object .entries(o) .map(([k, _]) => [k, transform(f, _)]) ) default: return f(o) }}const result = transform ( _ => sha256(String(_)) , {big: 20, baz: {foo: {cat: 3, bar: 5, qux: [1, 2, 3]}}} ) console.log(result).as-console-wrapper {max-height: 100% !important; top: 0}<script src="https://unpkg.com/js-sha256@0.9.0/src/sha256.js"></script>{ "big": "f5ca38f748a1d6eaf726b8a42fb575c3c71f1864a8143301782de13da2d9202b", "baz": { "foo": { "cat": "4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce", "bar": "ef2d127de37b942baad06145e54b0c619a1f22327b2ebbcfbec78f5564afe39d", "qux": [ "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b", "d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35", "4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce" ] } }}这种方法的一个明显优点是Array和Object分支可以以任何顺序出现。使用时Array.isArray(t)必须先检查后Object(t) === t检查。这是一件微妙的事情,但值得注意——// also correct!function transform (f, o){ switch (o?.constructor) { case Object: // <- type-check Object return // ... case Array: // <- type-check Array return // ... default: return f(o) }}您可能还希望对整个对象进行哈希处理。hash这是使用泛型函数实现泛型的一种可能性traverse-function* traverse (t, r = []){ switch (t?.constructor) // <- any t, even null and undefined { case Array: case Set: case Map: for (const [k, _] of t.entries()) yield* traverse(_, [...r, k]) break case Object: for (const [k, _] of Object.entries(t)) yield* traverse(_, [...r, k]) break default: yield [r, t] }}function hash (t){ const r = sha256.create() for (const [k, v] of traverse(t)) r.update(k.concat(v).join(":")) return r.hex()}const input = {big: 20, baz: {foo: {cat: 3, bar: 5, qux: [1, 2, 3]}}}console.log(hash("foo"), hash("foo"))console.log(hash([1,2,3]), hash([1,2,3]))console.log(hash(input), hash(input)).as-console-wrapper {max-height: 100% !important; top: 0}<script src="https://unpkg.com/js-sha256@0.9.0/src/sha256.js"></script>2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae492f06976c8bc705819f5d33d71be6a80a547b03f87c377e3543605d8260159c492f06976c8bc705819f5d33d71be6a80a547b03f87c377e3543605d8260159cd1ae8b8641d3d6d65b1e4eecab0484a9f9618f2aabafe473c8bb0b4f6382695cd1ae8b8641d3d6d65b1e4eecab0484a9f9618f2aabafe473c8bb0b4f6382695c