自实现vue
实现一个简易 Vue 框架
Vue 的核心功能包括数据响应式、模板编译和虚拟 DOM。以下是一个简化版的实现思路:
数据响应式系统
通过 Object.defineProperty 或 Proxy 实现数据劫持。当数据变化时通知依赖更新。
class Observer {
constructor(data) {
this.walk(data)
}
walk(data) {
if (!data || typeof data !== 'object') return
Object.keys(data).forEach(key => {
this.defineReactive(data, key, data[key])
})
}
defineReactive(obj, key, val) {
const dep = new Dep()
this.walk(val)
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get() {
Dep.target && dep.addSub(Dep.target)
return val
},
set(newVal) {
if (newVal === val) return
val = newVal
dep.notify()
}
})
}
}
依赖收集系统
实现一个发布-订阅模式来管理依赖关系。

class Dep {
constructor() {
this.subs = []
}
addSub(sub) {
this.subs.push(sub)
}
notify() {
this.subs.forEach(sub => sub.update())
}
}
Dep.target = null
观察者 Watcher
每个表达式对应一个 Watcher 实例,当数据变化时触发回调。
class Watcher {
constructor(vm, exp, cb) {
this.vm = vm
this.exp = exp
this.cb = cb
this.value = this.get()
}
get() {
Dep.target = this
const value = this.vm.data[this.exp]
Dep.target = null
return value
}
update() {
const oldValue = this.value
this.value = this.vm.data[this.exp]
this.cb.call(this.vm, this.value, oldValue)
}
}
编译器 Compiler
解析模板指令,初始化视图并绑定更新函数。

class Compiler {
constructor(el, vm) {
this.$vm = vm
this.$el = document.querySelector(el)
if (this.$el) {
this.compile(this.$el)
}
}
compile(el) {
const childNodes = el.childNodes
Array.from(childNodes).forEach(node => {
if (this.isElement(node)) {
this.compileElement(node)
} else if (this.isInterpolation(node)) {
this.compileText(node)
}
if (node.childNodes && node.childNodes.length > 0) {
this.compile(node)
}
})
}
isElement(node) {
return node.nodeType === 1
}
isInterpolation(node) {
return node.nodeType === 3 && /\{\{(.*)\}\}/.test(node.textContent)
}
compileElement(node) {
const attrs = node.attributes
Array.from(attrs).forEach(attr => {
const attrName = attr.name
const exp = attr.value
if (this.isDirective(attrName)) {
const dir = attrName.substring(2)
this[dir] && this[dir](node, exp)
}
})
}
compileText(node) {
this.update(node, RegExp.$1, 'text')
}
update(node, exp, dir) {
const updaterFn = updater[dir + 'Updater']
updaterFn && updaterFn(node, this.$vm[exp])
new Watcher(this.$vm, exp, function(value) {
updaterFn && updaterFn(node, value)
})
}
text(node, exp) {
this.update(node, exp, 'text')
}
model(node, exp) {
this.update(node, exp, 'model')
node.addEventListener('input', e => {
this.$vm[exp] = e.target.value
})
}
isDirective(attr) {
return attr.indexOf('v-') === 0
}
}
const updater = {
textUpdater(node, value) {
node.textContent = value
},
modelUpdater(node, value) {
node.value = value
}
}
Vue 主类
整合以上功能,创建 Vue 实例。
class Vue {
constructor(options) {
this.$options = options
this.$data = options.data
new Observer(this.$data)
Object.keys(this.$data).forEach(key => {
this.proxyData(key)
})
new Compiler(options.el, this)
}
proxyData(key) {
Object.defineProperty(this, key, {
get() {
return this.$data[key]
},
set(newVal) {
this.$data[key] = newVal
}
})
}
}
使用示例
<div id="app">
<p>{{ message }}</p>
<input v-model="message">
</div>
<script>
const vm = new Vue({
el: '#app',
data: {
message: 'Hello Mini Vue!'
}
})
</script>
实现要点总结
数据劫持通过 Object.defineProperty 实现
依赖收集系统管理数据变化时的更新
编译器解析模板并建立数据与视图的绑定
通过代理使数据可以直接通过 Vue 实例访问
这个简易实现包含了 Vue 的核心概念,实际 Vue 源码更加复杂,包含虚拟 DOM、组件系统、生命周期等更多功能。






