模拟数据监测
<!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>