Skip to content

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

Published: at 10:58 AMSuggest Changes

手写实现简单的 hash 路由

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)

Previous Post
使用原生 JavaScript 发送文件的几种方法
Next Post
摩拜单车:无需二维码解锁体验