老生常谈的双向数据绑定,再Vue2和Vue3中实现原理分别是Object.defineProperty和Proxy。
一个是Object原型上的方法,一个是类Proxy。下面就来看看叭。
Object.defineProperty
<!-- Object.defineProperty -->
<!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>
<input type="text" id="input">
<div id="box"></div>
<script>
//对象的四个特征
//configurable 是否可被删除
//enumerable 是否可以被 for in遍历
//value 当前属性值
//writable 是否可以被修改
let obj = {};
let input = document.getElementById('input')
let box = document.getElementById('box')
//数据劫持
Object.defineProperty(obj, 'text', {
configurable: true,
enumerable: true,
get() {
//获取数据就直接拿
console.log('获取数据了');
},
set(newValue) {
//修改数据就重新赋值
console.log('数据更新了');
input.value = newValue;
box.innerHTML = newValue
}
})
//输入监听
input.addEventListener('keyup', function (e) {
obj.text = e.target.value
})
</script>
</body>
</html>
Proxy
<!-- es6 proxy -->
<!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>
<input type="text" id="input">
<div id="box"></div>
<script>
let input = document.querySelector('#input')
let box = document.querySelector('#box')
const handle = {
get(obj, prop) {
console.log(`获取数据了`);
},
set(obj, prop, newValue) {
console.log(`数据更新了`);
obj[prop] = newValue
input.value = newValue;
box.innerHTML = newValue
}
}
const obj = new Proxy({}, handle)
input.addEventListener('keyup', function (e) {
obj.a = e.target.value
})
</script>
</body>
</html>
记录记录!