js实现vue
使用原生JavaScript实现Vue核心功能
Vue的核心功能包括数据响应式、模板编译和虚拟DOM。以下是使用原生JavaScript模拟Vue核心功能的实现方法。
数据响应式系统
通过Object.defineProperty实现数据劫持,当数据变化时通知依赖更新:

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())
}
}
实现编译器
简单的模板编译器,将模板中的插值表达式替换为实际数据:

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)
}
compileText(node) {
const exp = RegExp.$1.trim()
this.update(node, exp, 'text')
}
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)
}
})
}
isDirective(attr) {
return attr.startsWith('v-')
}
text(node, exp) {
this.update(node, exp, 'text')
}
html(node, exp) {
this.update(node, exp, 'html')
}
update(node, exp, dir) {
const updaterFn = this[dir + 'Updater']
updaterFn && updaterFn(node, this.$vm[exp])
new Watcher(this.$vm, exp, function(value) {
updaterFn && updaterFn(node, value)
})
}
textUpdater(node, value) {
node.textContent = value
}
htmlUpdater(node, value) {
node.innerHTML = value
}
}
实现Watcher
Watcher作为观察者,当依赖变化时执行更新:
class Watcher {
constructor(vm, key, cb) {
this.vm = vm
this.key = key
this.cb = cb
Dep.target = this
this.vm[this.key] // 触发getter,添加依赖
Dep.target = null
}
update() {
this.cb.call(this.vm, this.vm[this.key])
}
}
实现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>
<button v-text="btnText"></button>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!',
btnText: 'Click me'
}
})
// 修改数据
setTimeout(() => {
app.message = 'Data changed!'
}, 1000)
</script>
这个实现包含了Vue的核心功能:数据响应式、模板编译和依赖收集。实际Vue.js的实现更加复杂,包括虚拟DOM、组件系统、生命周期等更多功能,但以上代码展示了Vue的核心原理。






