码迷,mamicode.com
首页 > 其他好文 > 详细

[vue][原理]利用vue响应式原理实现一个时钟

时间:2020-05-14 13:33:57      阅读:54      评论:0      收藏:0      [点我收藏+]

标签:lang   obj   text   com   charset   har   html   OLE   ring   

关于vue2.x响应式原理核心内容可以查看这里

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>reactive timer</title>
</head>
<body>
<div id="app"></div>
<script>
// 数据响应式:
// Object.defineProperty()
const app = document.getElementById(‘app‘)
const obj = {}
function defineReactive(obj, key, val) {
  Object.defineProperty(obj, key, {
    get() {
      console.log(‘get‘, key)
      return val
    },
    set(newVal) {
      if (newVal !== val) {
        console.log(‘set‘, key)
        val = newVal
        update()
      }
    }
  })
}

defineReactive(obj, ‘foo‘, ‘‘)

function update() {
// dom 操作
  app.innerText = obj.foo.toLocaleTimeString()
}

setInterval(() => {
  obj.foo = new Date()
}, 1000)
</script>
</body>
</html>

[vue][原理]利用vue响应式原理实现一个时钟

标签:lang   obj   text   com   charset   har   html   OLE   ring   

原文地址:https://www.cnblogs.com/danker/p/12887713.html

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