码迷,mamicode.com
首页 > Web开发 > 详细

js proxy 代理模拟vue实现数据双向绑定

时间:2019-01-17 10:20:56      阅读:359      评论:0      收藏:0      [点我收藏+]

标签:content   get   com   targe   fun   cal   width   handler   init   

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Proxy vue 双向绑定</title>
  </head>

  <body>
    <div id="app">
      <h3 id="papagraph"></h3>
      <input type="text" id="input" />
    </div>
  </body>
</html>

<script>
  const papagraph = document.getElementById(‘papagraph‘);
  const input = document.getElementById(‘input‘);
 

  const data = {
    text: ‘hello‘
  }

  const handler = {
    set: function(target, prop, value) {
      if (prop === ‘text‘) {
        target[prop] = value
        papagraph.innerHTML = value
        return true;
      } else {
        return false;
      }
    }
  }
  
  const myTest = new Proxy(data, handler);

  input.addEventListener(
    ‘input‘,
    e => {
      myTest.text = e.target.value;
    },
    false
  )

  
</script>

  

js proxy 代理模拟vue实现数据双向绑定

标签:content   get   com   targe   fun   cal   width   handler   init   

原文地址:https://www.cnblogs.com/wilsunson/p/10280514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!