我学习微信小程序的时候,又遇到了这个问题,再次再复习下
<script>
window.addEventListener("load", function() {
var data = {
name: "blue",
age: 18
}
var _this = {};
for (var item in data) {
// dynamic add property
Object.defineProperty(_this, item, {
get() {
console.log("get...")
return data[item];
},
set(newVal) {
console.log("set...")
data[item] = newVal;
}
})
}
console.log(_this.name)
_this.name = "kk";
console.log(_this.name)
});
</script>