JS深拷贝

68 阅读1分钟
    const obj2 = {
      a: 1,
      b: 2,
    }
    obj2.c = obj2
    // 可迭代深拷贝
    function add(obj) {
      return new Promise((resolve, reject) => {
        const { port1, port2 } = new MessageChannel()
        port1.postMessage(obj)
        port2.onmessage = msg => {
          resolve(msg.data)
        }
      })
    }

    add(obj2).then(res => {
      console.log(res === obj2)
    })