手写vue的双向绑定

        姓名: <span id="spanName"></span>
        <br>
        <input type="text" id="iptName" />

关键的js代码:

        let obj = {
            name: ''
        }
        let spanName = document.getElementById('spanName')
        let iptName = document.getElementById('iptName')
        let newObj = JSON.parse(JSON.stringify(obj))
        Object.defineProperty(obj, 'name', {
            get() {
                return newObj.name
            },
            set(val) {
                if (val === newObj.name) return
                newObj.name = val
                observer()
            }
        })
        function observer() {
            spanName.innerHTML = obj.name
            iptName.value = obj.name
        }
        observer()
        setTimeout(() => {
            obj.name = "456"
        }, 1000)
        iptName.oninput = function () {
            obj.name = this.value
        }

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页