- vue数据双向绑定原理分析和简单实现
- 代码总共分为三个版本,v3为最终版本
canfoo / self-vue Goto Github PK
View Code? Open in Web Editor NEWvue数据双向绑定原理分析和简单实现
vue数据双向绑定原理分析和简单实现
eg.
el: '#app',
data: {
title: 'hello world',
name: 'canfoo',
obj: {
c: 3
}
},
此处 如果执行 obj.c = 4
, 不会被监听,因为我看到 watcher.js中 这段逻辑
var value = this.vm.data[this.exp];
var oldVal = this.value;
if (value !== oldVal) {
引用类型 , 不会进入此if判断。
****************************************分割线***************************************
问题2: 并且 compile.js无法编译 这样的东西 <h1>obj c: {{obj.c}}</h1>
1、 var dep = new Dep(); 这是消息订阅器放订阅者的集合容器,为什么要写在 defineReactive里面 这里递归后dep会被多次创建啊 难道dep不应该是只有一个消息订阅器吗?
2、 dep.notify(); 这里为什么要通知全部订阅者啊? 不应该是只通知与该数据字段变化所对应绑定的订阅者吗?
function defineReactive(data, key, val) {
observe(val); // 递归遍历所有子属性
var dep = new Dep();
Object.defineProperty(data, key, {
enumerable: true,
configurable: true,
get: function() {
if (Dep.target) {. // 判断是否需要添加订阅者
dep.addSub(Dep.target); // 在这里添加一个订阅者
}
return val;
},
set: function(newVal) {
dep.notify(); // 如果数据变化,通知所有订阅者
}
});
}
一个小bug,在compile.js中处理{{name}}时正则匹配后trim一下,不然没法处理{{ name }}这样的情况。谢谢。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.