Object.defineProperty()实现数据双向绑定

html:

<p class="text"></p>

js:

var data = {};
Object.defineProperty(data,'msg',{
  get: function(){
    return document.querySelector('.text').innerTHML;
  },
  set: function(newValue){
    document.querySelector('.text').innerHTML = newValue;
  }
})
data.msg = 'shen';

猜你喜欢

转载自www.cnblogs.com/shenxinze/p/9182654.html