-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue_learn.js
43 lines (38 loc) · 917 Bytes
/
vue_learn.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
34
35
36
37
38
39
40
41
42
43
function cb (val) {
console.log('test');
}
class Vue {
constructor(options) {
this._data = options.data;
this.observer(this._data);
}
}
Vue.prototype.observer = function (value) {
if (!value || (typeof value !== 'object')) {
return;
}
Object.keys(value).forEach((key) => {
this.definneReactive(value, key, value[key]);
});
}
Vue.prototype.definneReactive = function (obj, key, val) {
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get: function reactiveGetter () {
return val;
},
set: function reactiveSetter (newVal) {
console.log(newVal, val);
if (newVal === val) return;
// 每次赋值之后更新
val = newVal;
cb(newVal);
}
})
}
var test = new Vue({
data: {
test: '123'
}
});