高级vue教程 源码分析
function convert(obj) {
Object.keys(obj).forEach(key => {
let internalValue = obj[key]
Object.defineProperty(obj, key, {
get() {
console.log(`getting key "${key}": ${internalValue}`)
return internalValue
},
set(newValue) {
console.log(`setting key "${key}" to: ${newValue}`)
internalValue = newValue
}
})
})
}
let student = {
name: "liuyi",
age: 12,
sex: "female"
}
convert(student)
Object.keys(student).forEach(key => {
console.log("student.key", student[key]);
})
console.log("student.name", student.name);
console.log("student.age", student.age);
student.name = "xiaowang";
student.age = student.age + 1;
console.log("student.name", student.name);