Demo
// index.html
<body>
<div id="app">
<h1>{{song}}</h1>
<p>《{{album.name}}》是{{singer}}2005年11月发行的专辑</p>
<p>主打歌为{{album.theme}}</p>
<p>作词人为{{singer}}等人。</p>
为你弹奏肖邦的{{album.theme}}
</div>
<!--实现的mvvm-->
<script src="mvvm.js"></script>
<script>
// 写法和Vue一样
let mvvm = new Mvvm({
el: '#app',
data: { // Object.defineProperty(obj, 'song', '发如雪');
song: '发如雪',
album: {
name: '十一月的萧邦',
theme: '夜曲'
},
singer: '周杰伦'
}
});
</script>
</body>
打造 MVVM
// 创建一个Mvvm构造函数
// 这里用es6方法将options赋一个初始值,防止没传,等同于options || {}
function Mvvm(options = {}) {
// vm.$options Vue上是将所有属性挂载到上面
// 所以我们也同样实现,将所有属性挂载到了$options
this.$options = options;
// this._data 这里也和Vue一样
let data = this._data = this.$options.data;
// 数据劫持
observe(data);
}
数据劫持
为什么要做数据劫持?
- 观察对象,给对象增加 Object.defineProperty
- vue特点是不能新增不存在的属性,不存在的属性没有 get 和 set
- 深度响应,因为每次赋予一个新对象时会给这个新对象增加 defineProperty(数据劫持)
function observe(data) {
if (!data || typeof data !== 'object') return
return new Observe(data)
}
function Observe(data) {
for (let key in data) {
let val = data[key]
observe(val)
Object.defineProperty(data, key, {
configurable: true,
get () {
return val
},
set (newVal) {
if (val === newVal) {
return
}
val = newVal
observe(newVal)
}
})
}
}
数据代理
数据代理就是让我们每次拿data里的数据时,不用每次都写一长串,如mvvm._data.a.b这种,我们其实可以直接写成mvvm.a.b这种显而易见的方式
function Mvvm(options = {}) {
this.$options = options
let data = this._data = this.$options.data
// 数据劫持
observe(data)
// 数据代理
for (let key in data) {
Object.defineProperty(this, key, {
configurable: true,
get () {
return this._data[key]
},
set (newVal) {
this._data[key] = newVal
}
})
}
}
数据编译
function Mvvm(options = {}) {
this.$options = options
let data = this._data = this.$options.data
// 数据劫持
observe(data)
// 数据代理
for (let key in data) {
Object.defineProperty(this, key, {
configurable: true,
get () {
return this._data[key]
},
set (newVal) {
this._data[key] = newVal
}
})
}
// 编译
new Compile(options.el, this)
}
function Compile(el, vm) {
vm.$el = document.querySelector(el)
let fragment = document.createDocumentFragment()
while (child = vm.$el.firstChild) {
fragment.appendChild(child)
}
function replace (frag) {
Array.from(frag.childNodes).forEach(node => {
let txt = node.textContent;
let reg = /\{\{(.*?)\}\}/g; // 正则匹配{{}}
if (node.nodeType === 3 && reg.test(txt)) { // 即是文本节点又有大括号的情况{{}}
console.log(RegExp.$1); // 匹配到的第一个分组 如: a.b, c
let arr = RegExp.$1.split('.');
let val = vm;
arr.forEach(key => {
val = val[key]; // 如this.a.b
});
// 用trim方法去除一下首尾空格
node.textContent = txt.replace(reg, val).trim();
}
// 如果还有子节点,继续递归replace
if (node.childNodes && node.childNodes.length) {
replace(node);
}
});
}
replace(fragment)
vm.$el.appendChild(fragment)
}
function observe(data) {
if (!data || typeof data !== 'object') return
return new Observe(data)
}
function Observe(data) {
for (let key in data) {
let val = data[key]
observe(val)
Object.defineProperty(data, key, {
configurable: true,
get () {
return val
},
set (newVal) {
if (val === newVal) {
return
}
val = newVal
observe(newVal)
}
})
}
}
发布订阅
发布订阅主要靠的就是数组关系,订阅就是放入函数,发布就是让数组里的函数执行
// 发布订阅模式 订阅和发布 如[fn1, fn2, fn3]
function Dep() {
// 一个数组(存放函数的事件池)
this.subs = [];
}
Dep.prototype = {
addSub(sub) {
this.subs.push(sub);
},
notify() {
// 绑定的方法,都有一个update方法
this.subs.forEach(sub => sub.update());
}
};
// 监听函数
// 通过Watcher这个类创建的实例,都拥有update方法
function Watcher(fn) {
this.fn = fn; // 将fn放到实例上
}
Watcher.prototype.update = function() {
this.fn();
};
let watcher = new Watcher(() => console.log(111)); //
let dep = new Dep();
dep.addSub(watcher); // 将watcher放到数组中,watcher自带update方法, => [watcher]
dep.addSub(watcher);
dep.notify();
数据更新视图