<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
'use strict'
const a = {}
const b = {}
const observerObj = []
function observer(watcher, name) {
for (let i; i < watcher.length; i++) {
watcher.name = name
}
}
let bValue = '小名';
Object.defineProperty(a, 'name', {
get: function () {
return bValue;
},
set: function (newValue) {
bValue = newValue;
observer(observerObj, newValue)
},
})
bValue = '小蓝'
Object.defineProperty(b, 'name', {
get: function () {
return bValue;
},
set: function (newValue) {
bValue = newValue;
},
})
observerObj.push(b)
a.name = '我是小北'
console.log(b)
</script>
</body>
</html>