console
class Dep {
constructor(){
this.subs = []
}
addSub(sub){
this.subs.push(sub)
}
removeSub(sub){
remove(this.subs, sub)
}
depend(){
if(window.target){
this.addSub(window.target)
}
}
notify(){
const subs = this.subs.slice()
for(let i=0; i< this.subs.length; i++){
subs[i].update()
}
}
}
function remove(arr, item){
if(arr.length){
const index = arr.indexOf(item)
if(index > -1){
return arr.splice(index, 1)
}
}
}
function defineReactive(data, key, val){
let dep = new Dep()
Object.defineProperty(data, key, {
enumerable: true,
configurable: true,
get: function(){
dep.depend()
return val
},
set: function(newVal){
if(val == newVal){
return
}
dep.notify()
val = newVal
}
})
}
<html>
<head>
</head>
<body>
<p>Object的变化侦测</p>
</body>
</html>