模拟数据监测

<!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>

 

posted @ 2022-10-31 11:30  青Fire  阅读(14)  评论(0编辑  收藏  举报