Deep Cloning Objects in JavaScript, the Modern Way

2023-01-28

https://www.builder.io/blog/structured-clone

なるほど、使っていこう。

Deno で試してみた

$ deno
Deno 1.29.4
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> const a = {foo: "bar"}
undefined
> const b = structuredClone(a)
undefined
> console.log(JSON.stringify(a))
{"foo":"bar"}
undefined
> console.log(JSON.stringify(b))
{"foo":"bar"}
undefined
> a.foo = "baz"
"baz"
> console.log(JSON.stringify(a))
{"foo":"baz"}
undefined
> console.log(JSON.stringify(b))
{"foo":"bar"}
undefined

対応状況

モダンブラウザは全て対応済み。

Can I use

https://caniuse.com/mdn-api_structuredclone

Link