vue的数据双向绑定,是通过 Object.defineProperty();劫持属性来实现的 同时结合 发布者-订阅者(观察者模式!);来实现的完整的双向绑定
<input type="text" id="inputMsg">
<br>
<p id="msg"></p>
<script>
var obj={
msg:'hello world'
}
Object.defineProperty(obj,'msg',{
get:function(){
console.log('获取数值!');
},
set:function(val){
document.getElementById('inputMsg').value=val;
document.getElementById('msg').innerText=val;
}
})
document.getElementById('inputMsg').addEventListener('keyup',function(e){
obj.msg=e.target.value;
})
</script>