首页 > 其他分享 >模拟数据监测

模拟数据监测

时间:2022-10-31 11:47:36浏览次数:36  
标签:const keys Object vm obj 监测 数据 obs 模拟

<!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>数据监测</title>
</head>

<body>
    <script defer>
        const student = {
            id: 1,
            name: 'Tom',
            age: '18'
        };

        const obs = new Observer(student);
        const vm = obs;
        vm._data = data = obs;

        function Observer(obj) {
            const keys = Object.keys(obj)

            keys.forEach(k => {
                Object.defineProperty(this, k, {
                    get(){
                        return obj[k]
                    },
                    set(newValue){
                        console.log(`更新${k}值`)
                        obj[k] = newValue
                    }
                })
            })
        }
    </script>
</body>

</html>

 

标签:const,keys,Object,vm,obj,监测,数据,obs,模拟
From: https://www.cnblogs.com/qFire/p/16843736.html

相关文章