无所事事的样子开始了摸鱼的一天
Vue 发布订阅模式
透过事件中心的方式减少发布者和订阅者之间的关联
let vm = new Vue()
// 注册事件
vm.$on('event',()=>{
console.log('event')
})
vm.$on('event',()=>{
console.log('event1')
})
// 触发事件
vm.$emit('event')
// 实现简易的发布订阅模式
class EentEmitter{
constructor(){
this.subs=Object.create(null)
// Object.create(null)设置对象的原型为null,相比于 {} 提升性能
}
// 注册事件
$on(eventType,handler){
this.subs[eventType] = this.subs[eventType] ||[]
this.subs[eventType].push(handler)
}
// 触发事件
$emit(eventType){
if(this.subs[eventType]){
this.subs[eventType].forEach(handler=>{
handler()
})
}
}
}
观察者模式
没有事件中心,就是没有广播让所有人知道,么的群聊只有私聊
发布者和订阅者相互依存
class Dep{
construction(){
this.subs=[]
}
addSub(sub){
if(sub&&sub.update){
this.subs.push(sub)
}
}
notify(){
this.subs.forEach(sub=>{
sub.update()
})
}
}
class Watcher{
update(){
console.log('update')
}
}
let dep new Dep()
let watcher = new Watcher()
dep.addSub(watcher)
dep.notify()
Vue响应式原理(实现最小版本的Vue)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<div>msg</div>
<div>{{msg}}</div>
<div>v-text msg</div>
<div v-text="msg"></div>
<div>count</div>
<div>{{count}}</div>
<label>
<input type="text" v-model="count">
</label>
</div>
<script src="dep.js"></script>
<script src="watcher.js"></script>
<script src="compiler.js"></script>
<script src="observer.js"></script>
<script src="vue.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
msg: 'asd',
count: 22
}
});
</script>
</body>
</html>
//vue.js
class Vue {
constructor(options) {
// 通过属性保存选项数据
this.$options = options || {};
this.$data = options.data || {};
this.$el = typeof options.el === 'string' ? document.querySelector(options.el) : options.el;
// 把data中的成员转换成getter setter 注入vue实例
this._proxyData(this.$data);
// 调用observer监听数据变化
new Observer(this.$data)
// 解析插值表达式
new Compiler(this)
}
_proxyData(data) {
// 遍历data属性
Object.keys(data)
.forEach(key => {
Object.defineProperty(this, key, {
enumerable: true,
configurable: true,
get() {
return data[key];
},
set(v) {
if (v === data[key]) {
return;
}
data[key] = v;
}
});
});
}
}
//observer.js
class Observer {
constructor(data) {
this.walk(data);
}
walk(data) {
// 判断data是否为对象
if (!data || typeof data !== 'object') {
return;
}
// 遍历data对象
Object.keys(data)
.forEach(key => {
this.defineReactive(data, key, data[key]);
});
}
defineReactive(obj, key, val) {
let that = this;
let dep = new Dep();
// 如果val是对象 则讲起转换为响应式数据
this.walk(val);
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get() {
Dep.target && dep.addSub(Dep.target);
return val;
},
set(v) {
if (v === val) {
return;
}
val = v;
that.walk(v);
dep.notify()
}
});
}
}
//compiler.js
class Compiler {
constructor(vm) {
this.el = vm.$el;
this.vm = vm;
this.compiler(this.el);
}
// 编译模板,处理文本和元素节点
compiler(el) {
let childNodes = el.childNodes;
Array.from(childNodes)
.forEach(node => {
// 处理文本节点
if (this.isTextNode(node)) {
this.compilerText(node);
}
// 处理元素节点
else if (this.isElementNode(node)) {
this.compilerElement(node);
}
// 判断是否有子节点 递归调用处理节点
if (node.childNodes && node.childNodes.length) {
this.compiler(node);
}
});
}
// 编译元素节点,处理指令
compilerElement(node) {
Array.from(node.attributes)
.forEach(attr => {
let attrName = attr.name;
if (this.isDirective(attrName)) {
attrName = attrName.substr(2);
let key = attr.value;
this.update(node, key, attrName);
}
});
}
update(node, key, attrName) {
// 拼接方法名称 减少if判断 并且便于更新
let updateFn = this[attrName + 'Updater'];
updateFn && updateFn.call(this, node, this.vm[key], key);
}
textUpdater(node, value, key) {
node.textContent = value;
new Watcher(this.vm, key, (newValue) => {
node.textContent = newValue;
});
}
modelUpdater(node, value, key) {
node.value = value;
new Watcher(this.vm, key, (newValue) => {
node.value = newValue;
});
}
// 编译文本节点,处理插值表达式
compilerText(node) {
let reg = /{{(.+?)}}/;
let value = node.textContent;
if (reg.test(value)) {
let key = RegExp.$1.trim();
node.textContent = value.replace(reg, this.vm[key]);
// watcher对象 当数据改变更新视图
new Watcher(this.vm, key, (newValue) => {
node.textContent = newValue;
});
}
}
isDirective(attrName) {
return attrName.startsWith('v-');
}
isTextNode(node) {
// nodeType =3 为文本节点
return node.nodeType === 3;
}
isElementNode(node) {
// nodeType =1 为元素节点
return node.nodeType === 1;
}
}
dep.js
class Dep {
constructor() {
this.subs = [];
}
addSub(sub) {
if (sub && sub.update) {
this.subs.push(sub);
}
}
notify(){
this.subs.forEach(sub=>{
sub.update()
})
}
}
//watcher.js
class Watcher {
constructor(vm, key, cb) {
this.vm = vm;
this.key = key;
// 回调函数负责的视图
this.cb = cb;
// 记录watcher对象
Dep.target = this;
// 触发get
this.oldValue = vm[key];
Dep.target = null;
}
update() {
let newValue = this.vm[this.key];
if (this.oldValue === newValue) {
return;
}
this.cb(newValue);
}
}
—————————————————————————————————————————————————————— 我这废柴