vue2中底层响应式数据实现
<input type="text" id="textInput" />输入:<span id="textSpan"></span> <script type="text/javascript"> var obj = {}; textInput = document.querySelector("#textInput"); textSpan = document.querySelector("#textSpan"); Object.defineProperty(obj, "foo", { set: function (newValue) { textInput.value = newValue; textSpan.innerHTML = newValue; }, }); textInput.addEventListener('keyup', function(e) { obj.foo = e.target.value; }); </script>
Object.defineProperty();
接收三个参数:
1.属性所在的对象
2.属性的名字
3.一个描述符对象