html_and_vue/vue/数据代理.html

28 lines
704 B
HTML
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数据代理</title>
</head>
<body>
<!-- 数据代理: 通过一个对象代理另一个对象中属性的操作(读/写) -->
<script type="text/javascript" >
let obj1 = {
x:100
}
let obj2 = {
y:200
}
Object.defineProperty(obj2,'x',{ //往obj2身上添加一个x属性其值为obj1的x属性值
get(){
return obj1.x
},
set(value){
obj1.x = value
}
})
</script>
</body>
</html>