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.一个描述符对象

posted @ 2023-02-16 10:44  富言杰  阅读(11)  评论(0编辑  收藏  举报