浅拷贝3(扩展运算符)

DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Documenttitle>
  head>
  <body>
    
    <script>
      /* 基本类型 */
      let obj1 = {
        name: 'Tim',
        age: 12,
        sex: 'male',
      }

      let obj2 = { ...obj1 }
      obj2.age = 16

      console.log(obj1) // {name: 'Tim', age: 12, sex: 'male'}
      console.log(obj2) // {name: 'Tim', age: 16, sex: 'male'}

      /* 引用类型 */
      let a = {
        info: {
          name: 'Tim',
          age: 12,
          sex: 'male',
        },
      }

      let b = { ...a }
      b.info.age = 16

      console.log(a) // {info:{name: 'Tim', age: 16, sex: 'male'}}
      console.log(b) // {info:{name: 'Tim', age: 16, sex: 'male'}}
    script>
  body>
html>

你可能感兴趣的:(代码,javascript,typescript,vue.js)