-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvue.js
33 lines (31 loc) · 1.09 KB
/
vue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Vue {
constructor(options) {
// 1. 通过属性保存选项的数据
this.$options = options || {}
this.$data = options.data || {}
// 如果是字符串就说明是选择器
this.$el = typeof options.el === 'string' ? document.querySelector(options.el) : options.el
// 2. 把data的成员转化为getter和setter注入到vue实例
this._proxyData(this.$data)
// 3. 调用observer对象,把data属性转化为响应式数据,监听数据的变化
new Observer(this.$data)
// 4. 调用Compiler对象,处理模版编译
new Compiler(this)
}
_proxyData(data) {
// 遍历对象
Object.keys(data).forEach((key) => {
Object.defineProperty(this, key, {
enumerable: true,
configurable: true,
get() {
return data[key]
},
set(newValue) {
if (newValue === data[key]) return
data[key] = newValue
}
})
})
}
}