0%

JS 手写简单的数据双向绑定

手写实现简单的 hash 路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
let obj = {};
let inputDom = document.getElementById('input');
let spanDom = document.getElementById('span');

Object.defineProperty(obj, 'text', {
configurable: true,
enumerable: true,
get: function () {
console.log('获取数据');
},
set: function (value) {
console.log('更新数据', value);
inputDom.value = value;
spanDom.innerText = value;
},
});

inputDom.addEventListener('keyup', function (e) {
let value = e.target.value;
obj.text = value;
})

setTimeout(() => {
obj.text = 123;
},1000)