vue3之reactive底层Proxy原理

作者: ropon 分类: Vue 发布时间: 2022-05-26 10:15
<!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>Document</title>
</head>

<body>
  <script>
    let person = {
      name: 'ropon',
      age: 18
    }
    const p = new Proxy(person, {
      get (target, propsName) {
        console.log(target, propsName)
        console.log(`有人读取了p身上${propsName}的属性`)
        return target[propsName]
      },
      set (target, propsName, value) {
        console.log(`有人设置或新增了p身上${propsName}属性,值是${value}`)
        target[propsName] = value
      },
      deleteProperty (target, propsName) {
        console.log(`有人删除了p身上${propsName}属性`)
        return delete target[propsName]
      }
    })
  </script>
</body>

</html>

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!