function vue(){
this.data = {
content:'双向绑定开始'
}
this.el = document.getElementById('app')
this._html = ''
this.observe(this.data)
this.render()
}
vue.prototype.observe = function(obj){
var _this = this
this.data = new Proxy(this.data,{
get:function(target,key){
return target[key]
},
set: function(target,key,newValue){
target[key] = newValue
_this.render()
}
})
}
vue.prototype.render = function(){
this._html = 'attention:' + this.data.content
this.el.innerHTML = this._html
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script src="vue3数据双向绑定原理.js"></script>
<script>
var vm = new vue()
setTimeout(()=>{
console.log('双向绑定已执行')
console.log(vm.data)
vm.data.content = '双向绑定结束'
},1000)
</script>
</body>
</html>