双向绑定 🌕 发表于 2017-06-23 | 分类于 知识点 | 阅 概述其实,双向绑定的实现就是Object.defineProperty的一种使用示例。 html1234567891011<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>双向绑定</title></head><body> <input id="a"> <p id="b"></p></body></html> js1234567891011121314151617181920var $a = document.getElementById("a");var $b = document.getElementById("b");var obj = {};Object.defineProperty(obj, "content", { get: function() { //console.log('get...') }, set: function(val) { $b.textContent = val; //console.log('set...') }});$a.addEventListener("input", function() { obj.content = this.value;});// init$a.value = $b.textContent = obj.content = "123";