Vue数据双向绑定
<!DOCTYPE html>
<html>
<head>
<title>数据绑定</title>
</head>
<body>
<div id="app">
<div>{{msg}}</div>
</div>
</body>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el: '#app',data: {
msg: 'Data'
},created: function() {
console.log(this.$data); //{__ob__: Observer}
}
})
</script>
</html>
/*
{__ob__: Observer}
msg: "Data"
__ob__: Observer {value: {…},dep: Dep,vmCount: 1}
get msg: ƒ reactiveGetter()
set msg: ƒ reactiveSetter(newVal)
__proto__: Object
*/
-
Observer
: 这里的主要工作是递归地监听对象上的所有属性,在属性值改变的时候,触发相应的Watcher
。
-
Watcher
: 观察者,当监听的数据值修改时,执行响应的回调函数,在Vue
里面的更新模板内容。
-
Dep
: 链接Observer
和Watcher
的桥梁,每一个Observer
对应一个Dep
,它内部维护一个数组,保存与该Observer
相关的Watcher
。
function __dep(){
this.subscribers = [];
this.addSub = function(watcher){
if(__dep.target && !this.subscribers.includes(__dep.target) ) this.subscribers.push(watcher);
}
this.notifyAll = function(){
this.subscribers.forEach( watcher => watcher.update());
}
}
function __observe(obj){
for(let item in obj){
let dep = new __dep();
let value = obj[item];
if (Object.prototype.toString.call(value) === "[object Object]") __observe(value);
Object.defineProperty(obj,item,{
configurable: true,enumerable: true,get: function reactiveGetter() {
if(__dep.target) dep.addSub(__dep.target);
return value;
},set: function reactiveSetter(newVal) {
if (value === newVal) return value;
value = newVal;
dep.notifyAll();
}
});
}
return obj;
}
function __watcher(fn){
this.update = function(){
fn();
}
this.activeRun = function(){
__dep.target = this;
fn();
__dep.target = null;
}
this.activeRun();
}
<!DOCTYPE html>
<html>
<head>
<title>数据绑定</title>
</head>
<body>
<div id="app">
<div>{{msg}}</div>
<div>{{date}}</div>
</div>
</body>
<script type="text/javascript">
var Mvvm = function(config) {
this.$el = config.el;
this.__root = document.querySelector(this.$el);
this.__originHTML = this.__root.innerHTML;
function __dep(){
this.subscribers = [];
this.addSub = function(watcher){
if(__dep.target && !this.subscribers.includes(__dep.target) ) this.subscribers.push(watcher);
}
this.notifyAll = function(){
this.subscribers.forEach( watcher => watcher.update());
}
}
function __observe(obj){
for(let item in obj){
let dep = new __dep();
let value = obj[item];
if (Object.prototype.toString.call(value) === "[object Object]") __observe(value);
Object.defineProperty(obj,{
configurable: true,get: function reactiveGetter() {
if(__dep.target) dep.addSub(__dep.target);
return value;
},set: function reactiveSetter(newVal) {
if (value === newVal) return value;
value = newVal;
dep.notifyAll();
}
});
}
return obj;
}
this.$data = __observe(config.data);
function __proxy (target) {
for(let item in target){
Object.defineProperty(this,get: function proxyGetter() {
return this.$data[item];
},set: function proxySetter(newVal) {
this.$data[item] = newVal;
}
});
}
}
__proxy.call(this,config.data);
function __watcher(fn){
this.update = function(){
fn();
}
this.activeRun = function(){
__dep.target = this;
fn();
__dep.target = null;
}
this.activeRun();
}
new __watcher(() => {
console.log(this.msg,this.date);
})
new __watcher(() => {
var html = String(this.__originHTML||'').replace(/"/g,'\\"').replace(/\s+|\r|\t|\n/g,' ')
.replace(/\{\{(.)*?\}\}/g,function(value){
return value.replace("{{",'"+(').replace("}}",')+"');
})
html = `var targetHTML = "${html}";return targetHTML;`;
var parsedHTML = new Function(...Object.keys(this.$data),html)(...Object.values(this.$data));
this.__root.innerHTML = parsedHTML;
})
}
var vm = new Mvvm({
el: "#app",data: {
msg: "1",date: new Date(),obj: {
a: 1,b: 11
}
}
})
</script>
</html>
https://github.com/WindrunnerMax/EveryDay
https://www.jianshu.com/p/255d4dec710a
https://www.jianshu.com/p/c8186e9e027b
https://www.cnblogs.com/wangjiachen666/p/9883916.html
https://blog.csdn.net/wangshu696/article/details/84570886
https://blog.csdn.net/qq_43051529/article/details/82877673
https://github.com/liutao/vue2.0-source/blob/master/%E5%8F%8C%E5%90%91%E6%95%B0%E6%8D%AE%E7%BB%91%E5%AE%9A.md