JS-简单数据绑定-MVC

一、视图层V

1
2
3
4
<div>
<p>你好,<span id='nickName'></span></p>
<div id="introduce"></div>
</div>


二、视图控制器C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var userInfo = {};


Object.defineProperty(userInfo, "nickName", {
get: function() {
return document.getElementById('nickName').innerHTML;
},
set: function(nick) {
document.getElementById('nickName').innerHTML = nick;
}
});


Object.defineProperty(userInfo, "introduce", {
get: function() {
return document.getElementById('introduce').innerHTML;
},
set: function(introduce) {
document.getElementById('introduce').innerHTML = introduce;
}
})


三、数据M

1
2
userInfo.nickName = "xxx";
userInfo.introduce = "我是xxx,我来自云南,..."