浅谈vue中数据双向绑定的实现原理
作者:萝卜爱吃青菜 时间:2023-07-02 16:52:51
vue中最常见的属v-model这个数据双向绑定了,很好奇它是如何实现的呢?尝试着用原生的JS去实现一下。
首先大致学习了解下Object.defineProperty()这个东东吧!
* Object.defineProperty()
* 对对象的属性进行 定义/修改
* */
let obj = {x:10}
// 这两种方式都相对来说比较简单,直接,但是有些时候我们需要对对象的属性的修改和增加进行必要的干预
// obj.y = 20;
// obj.x = 100;
// obj.x = 'abc';
//
// let arr = [1,2,3];
// arr.length = 'abc';//不可更改
// console.log(arr);
// console.log(obj.x);
// delete obj.x;
// console.log(obj);
Object.defineProperty(obj, 'y', {
configurable: false, //设置是否可删除 false为不可删除
value: 100
});
console.log(obj);
delete obj.y;//删除
console.log(obj);
//设置对象某个属性值的时候,顺便设置它的属性。enumerable 可枚举 configurable 可以删除否 writable 可改值否
Object.defineProperty(obj, 'z', {//enumerable 可枚举(没有则新添加)
enumerable: true,//为false时,for..in object.keys json.stringfy 不能取到该z属性
value: 10000
});
for (var attr in obj) {
console.log(attr);
}
Object.defineProperty(obj, 'm', {
writable: false,//可更改
value: 9
});
console.log(obj);
obj.m = 100;
console.log(obj);
以上总结了对象的defineProperty四个属性:configurable,enumerable,value,writable
接下来再深入认识下它的另外两个方法:set 以及get
注意:get和set不能与configurable,enumerable,value,writable同时存在
let obj = {x:10}
let y = 100;
Object.defineProperty(obj, 'y', {
get() {
//当obj的y属性被调用的时候触发,该方法的返回值将作为获取的结果
console.log('get');
return y;
},
set(value) {
//当obj的y属性被设置的时候触发
console.log('set', value);
y = value;
}
})
console.log(obj.y);
obj.y = 1;
console.log(obj.y);
介绍完defineProperty了,最后我们一起看看如何简单的实现数据双向绑定吧!
<body>
<input type="text" id="age">
<h1></h1>
<script>
var ageElement = document.querySelector('#age');
var h1Element = document.querySelector('h1');
let obj = {};
Object.defineProperty(obj, 'age', {
get() {
},
set(value) {
ageElement.value = value;
h1Element.innerHTML = value;
}
})
obj.age = 10;
ageElement.oninput = function() {
obj.age = this.value;
}
</script>
来源:http://www.cnblogs.com/hjj2ldq/p/7518498.html?utm_source=tuicool&utm_medium=referral
标签:vue,双向绑定
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现的生产者、消费者问题完整实例
2022-08-15 14:37:30
python中使用正则表达式的连接符示例代码
2021-07-08 09:34:43
Mysql常用运算符与函数汇总
2024-01-17 21:36:02
matplotlib交互式数据光标实现(mplcursors)
2022-05-02 14:11:30
![](https://img.aspxhome.com/file/2023/0/80670_0s.png)
python批量处理打开多个文件
2022-10-21 05:26:47
![](https://img.aspxhome.com/file/2023/0/113030_0s.png)
win7下mysql6.x出现中文乱码的完美解决方法
2024-01-17 03:36:45
python利用pd.cut()和pd.qcut()对数据进行分箱操作
2022-03-26 07:57:45
vue踩坑日记之params传递参数问题
2024-05-10 14:19:48
![](https://img.aspxhome.com/file/2023/7/126407_0s.png)
Typora自动编号的具体操作
2022-01-21 03:20:18
![](https://img.aspxhome.com/file/2023/8/114688_0s.png)
基于Python实现简易的动漫图片转换器
2022-12-25 23:23:11
![](https://img.aspxhome.com/file/2023/4/117604_0s.png)
python 实现单通道转3通道
2021-09-12 22:03:25
word-wrap同word-break的区别
2007-10-24 20:08:00
ASP.NET MVC Bundles 用法和说明(打包javascript和css)
2023-07-17 01:20:59
浅谈Pandas:Series和DataFrame间的算术元素
2023-02-02 18:52:52
python如何读取和存储dict()与.json格式文件
2021-12-07 16:13:36
![](https://img.aspxhome.com/file/2023/6/90326_0s.png)
MySQL重定位数据目录的方法
2024-01-19 02:51:57
ASP生成静态页面的方法
2010-05-27 12:13:00
详解前端自动化工具gulp自动添加版本号
2023-08-09 14:48:41
详解如何修改jupyter notebook的默认目录和默认浏览器
2022-07-01 14:34:54
![](https://img.aspxhome.com/file/2023/7/69017_0s.png)
vue实现点击图片放大效果
2024-05-21 10:30:28