数据双向绑定是如何实现的?
Vue.js是通过数据劫持结合发布者订阅者的模式来完成双向数据绑定的,会通过Object.defineProperty()来劫持各个属性的getter和setter,当数据发生变动时,发布消息给订阅者,并触发相应的监听回调。
vue.js文件
class Vue{
constructor(options){
this.$data = options.data
// 调用数据劫持的方法
Observe(this.$data)
// 属性代理
Object.keys(this.$data).forEach(key=>{
Object.defineProperty(this,key,{
enumerable:true,
configurable:true,
get(){
return this.$data[key]
},
set(newValue){
this.$data[key] = newValue
}
})
})
// 调用模板编译的函数
Compile(options.el,this)
}
}
// 定义一个数据劫持的方法
function Observe(obj){
// 递归的终止条件
if(!obj || typeof obj !== 'object') return
const dep = new Dep()
// 通过Object.keys(obj) 获取到当前obj上的每个属性
Object.keys(obj).forEach(key=>{
// 当前被循环的key所对应的属性值
let value = obj[key]
// 把value这个子节点,进行递归
Observe(value)
// 需要为当前的key所对应的属性,添加getter和setter
Object.defineProperty(obj,key,{
enumerable:true,
configurable:true,
get(){
Dep.target && dep.addSub(Dep.target)
console.log(`有人读取了${key}的值`);
return value
},
set(newVal){
value = newVal
Observe(value)
dep.notify()
},
})
})
}
// 对HTML结构进行模板编译的方法
function Compile(el,vm){
// 获取el对应的DOM元素
vm.$el = document.querySelector(el)
const fragment = document.createDocumentFragment()
while((childNode = vm.$el.firstChild)){
fragment.appendChild(childNode)
}
// 进行模板编译
replace(fragment)
vm.$el.appendChild(fragment)
function replace(node){
// 定义匹配插值表达式的正则
const regMustache = /{{s*(S+)s*}}/
// 证明当前的node节点是一个文本子节点,需要进行正则的替换
if(node.nodeType===3){
// 注意:文本子节点,也是一个DOM对象,如果要获取文本子节点的字符串内容,需要调用textContent属性获取
const text = node.textContent
const execResult = regMustache.exec(text)
if(execResult){
const value = execResult[1].split('.').reduce((newObj,k)=>newObj[k],vm)
node.textContent = text.replace(regMustache,value)
new Watcher(vm,execResult[1],(newValue)=>{
node.textContent = text.replace(regMustache,newValue)
})
}
// 终止递归的条件
return
}
// 判断当前的node节点是否为input输入框
if(node.nodeType===1 && node.tagName.toUpperCase() === 'INPUT'){
const attrs = Array.from(node.attributes)
const findResult = attrs.find((x)=>x.name==='v-model')
if(findResult){
const expStr = findResult.value
const value = expStr.split('.').reduce((newObj,k)=>newObj[k],vm)
node.value = value
new Watcher(vm,expStr,(newValue)=>{
node.value = newValue
})
// 监听文本框的input输入事件,拿到文本框最新的值,把最新的值,更新到vm上即可
node.addEventListener('input',(e)=>{
const keyArr = expStr.split('.')
const obj = keyArr.slice(0,keyArr.length-1).reduce((newObj,k)=>newObj[k],vm)
obj[keyArr[keyArr.length-1]] = e.target.value
})
}
}
// 证明不是文本节点,可能是一个DOM元素,需要进行递归处理
node.childNodes.forEach(child=>replace(child))
}
}
// 收集依赖/收集订阅者
class Dep{
constructor(){
// 今后,所有的watcher都要存到这个数组中
this.subs = []
}
// 向 subs 数组中,添加watcher的方法
addSub(watcher){
this.subs.push(watcher)
}
// 负责通知每个watcher的方法
notify(){
this.subs.forEach(watcher=>watcher.update())
}
}
class Watcher{
constructor(vm,key,cb){
this.vm = vm
this.key = key
this.cb = cb
Dep.target = this
key.split('.').reduce((newObj,k)=>newObj[k],vm )
Dep.target = null
}
update(){
const value = this.key.split('.').reduce((newObj,k)=>newObj[k],this.vm)
this.cb(value)
}
}
测试用的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">
<h3>姓名:{{name}}</h3>
<h3>年龄:{{age}}</h3>
<h3>info.a的值是:{{info.a}}</h3>
<div>name的值:<input type="text" v-model="name"></div>
<div>info.a的值:<input type="text" v-model="info.a"></div>
</div>
<script src="./vue.js"></script>
<script>
const vm = new Vue({
el:"#app",
data:{
name:'zs',
age:20,
info:{
a:'a1',
c:'c1'
}
}
})
</script>
</body>
</html>