# Object.defineProperty 核心API
之所以说是核心API 是因为Vue2.x的响应式系统就是基于这个Api来实现的
下面我们也基于这个API来自己实现一个响应式系统
class Vue {
constructor(option) {
this._data = option.data
this.obvers();
}
obvers() {
const data = this._data;
if (!data || typeof data != 'object') return false
for (const key in data) {
if (data.hasOwnProperty(key)) {
this.definepro(data, key, data[key])
}
}
}
definepro(obj, key, value) {
const _this = this;
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get() {
return value
},
set(newval) {
_this.change(value, newval)
}
})
}
change(value, newval) {
console.log(`我把${value}跟新为${newval}`)
}
}
var vue = new Vue({
data: {
name: 'lyh'
}
})
vue._data.name = 1;
← vue运行机制