----
// pages/three/three.js
Page({
/**
*
*/
data: {
name:"",
age:"",
sex:""
},
onLoad: function (options) {
this.setWatch(this);
console.log(this)
},
watch:{
name(){
console.log("name")
console.log(111111)
},
age(){
console.log(this.data.name)
console.log("age")
},
sex(){
console.log("sex")
}
},
onReady: function () {
},
btn(){
this.setData({
name:1
})
},
setWatch(){
let watch=this.watch;
let _this=this;
// Object.keys(watch).forEach(v=>{
// console.log(v)
// var setVal = _this.data[v];
// Object.defineProperty(_this.data,v,{
// configurable: true,
// enumerable: true,
// get:function(){
// return setVal
// },
// set:function(val){
// setVal=val;
// _this.watch[v].call(_this);
// }
// })
// })
for(var v in watch){
console.log(v)
var setVal=_this.data[v];
Object.defineProperty(_this.data,v,{
configurable: true,
enumerable: true,
get:function(){
return setVal
},
set:function(val){
setVal=val;
console.log(_this.watch[v])
_this.watch[v].call(_this);
}
})
}
}
})
when I use for (var i in obj) {} to add get and set methods to an object, the listening event is always the last method in the watch object, that is, whether I modify the value of name or age, I execute the sex method in watch, but when I add it with Object.keys (obj), the event is normal and ask the boss to solve