<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>双向数据绑定原理</title>
</head>
<body>
<input type="text" id="input">
<script>
let obj = {};
let temp = {}
Object.defineProperty(obj,"name",{
get() { // 页面加载的时候调用
return temp["name"];
},
set(val) { // 页面数据变化的时候调用
temp["name"] = val;
input.value = val;
}
})
input.value = obj.name;
input.addEventListener("input",function(){
obj.name = this.value;
})
</script>
</body>
</html>