<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Proxy双向绑定</title>
</head>
<body>
<input id="input_el" oninput="inputHandler(this)">
<br/>
<div id="show_el"></div>
</body>
</html>
<script>
function proxy_bind(target){
return new Proxy(target, {
get(obj, props){
console.log('get' + obj[props])
if(!obj[props]){
obj[props] = 'hello'
}
return obj[props]
},
set(obj, props, newValue){
obj[props] = newValue
document.getElementById('show_el').innerText = newValue
return
}
})
}
let obj = {}
let obj_bind = proxy_bind(obj)
function inputHandler (c){
console.log(c.value)
obj_bind.value = c.value
}
;(function (){
document.getElementById('show_el').innerText = obj_bind.value
document.getElementById('input_el').value = obj_bind.value
})()
</script>
11-08
2662
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交