<body>
<div id="test1"></div>
<div id="test2"></div>
</body>
<script>
//创建对象
var user1 = new User();
user1.showInfo("test1");
var user2 = new User(null, "韩梅梅", "hanmeimei", 2, new Date(1995, 5-1, 1));
user2.showInfo("test2");
/* 声明构造函数 */
//user对象的构造函数
function User(id, userName, password, gender, birthday) {
this.id = id || "";
this.userName = userName || "佚名";
this.password = password || "0000";
this.gender = gender || 0;
this.birthday = birthday || "";
this.showInfo = function (idName) {
var msg = "<hr>"
+ "用户信息如下:<br>"
+ "id: " + this.id + "<br>"
+ "用户名: " + this.userName + "<br>"
+ "密码: " + this.password + "<br>"
+ "性别: " + this.genderName() + "<br>"
+ "生日: " + this.birthday + "<br>"
+ "年龄: " + this.age() + "<hr>";
document.getElementById(idName).innerHTML = msg;
}
this.genderName = function () {
if (this.gender == 1) return "男";
if (this.gender == 2) return "女";
return "保密";
}
this.age = function () {
//计算过程略...
return 18;
}
}
</script>