<body>
<div id = 'app'>
</div>
</body>
<script>
let smz = new SMZ({
el:'#app',
data:{
a:1
}
})
function SMZ(options={}){
this.$options = ooptions;
var data = this._data = this.$options.data;
//监听说有数据data
oberver(data)
}
function observer(data){
//判断data是否是object类型
if(typeof data !=='object ') return
return new Observer(data)
}
function Observer(data){
for( let key in data){
let value = data[key];
//如果对象包含对象,继续循环调用
observer(value);
Object.defineProperty(data,key,{
enumerable:true,//可枚举
get(){
return value;
},
set(newValue){
value = newValue;
//如果添加的值为对象,继续循环
observe(newValue);
}
})
}
}
</script>