javascript代码
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="UTF-8">
<title>MVVM</title>
</head>
<body>
<input id="user.name" type="text" > 姓名
<input id="user.age" type="text" > 年龄
<script type="text/javascript">
function User(){
var user = this;
var u_name = document.getElementById('user.name');
var u_age = document.getElementById('user.age');
u_name.addEventListener('change', function(){ user.name = this.value; });
u_age.addEventListener('change', function(){ user.age = this.value; });
Object.defineProperties(user,{
name : {
get : function(){ return name; },
set : function(new_name){ name = new_name; u_name.value = name; }
},
age : {
get : function(){ return age; },
set : function(new_age){ age = new_age; u_age.value = age; }
}
});
return user;
};
var user = new User();
user.name = '启东';
</script>
</body>
</html>