今天用原生js来实现一个数据的双向绑定

知识点:object.defineProperty()

  三个参数:

    第一个:要定义属性的对象,

    第二个:要定义或者修改的属性的名称

    第三个:将被定义或者修改的属性的描述

 

<input type="text" id="inp"/>
<p ></p>
<script>
  var obj = {};
  Object.defineProperty(obj,"newProp",{
    get:function(){
      return obj;
    },
    set:function(newVal){
      document.getElementById("inp").value = newVal;
      document.getElementById("showText").innerHTML = newVal;
    }
  })
  document.addEventListener("keyup",function(e){
    obj.newProp = e.target.value;
  })
</script>

 

相关文章:

  • 2021-05-24
  • 2022-12-23
  • 2021-07-15
  • 2021-09-28
  • 2022-12-23
  • 2022-02-08
  • 2022-02-27
猜你喜欢
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
  • 2018-09-29
  • 2021-07-24
  • 2021-07-05
  • 2018-04-19
相关资源
相似解决方案