<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>计ç®</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<script>
function student(serialNumber, name, sex, birthday, score) {
this.serialNumber = serialNumber;
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.score = score;
}
student.prototype.getAge = function() {
var birthday = new Date(this.birthday.replace(/-/g, "\/"));
var d = new Date();
var age =
d.getFullYear() -
birthday.getFullYear() -
(d.getMonth() < birthday.getMonth() ||
(d.getMonth() == birthday.getMonth() &&
d.getDate() < birthday.getDate())
? 1
: 0);
return age;
}
student.prototype.study = function() {
this.score++;
}
var obj = new student(1, 'å¼ ä¸', 'ç·', '1990-01-01', 65);
console.log('å
¥åï¼', 1, 'å¼ ä¸', 'ç·', '1990-01-01', 65);
console.log('å¹´é¾ï¼', obj.getAge());
obj.study();
console.log('å¦ä¹ ï¼', obj.score);
obj.study();
console.log('å¦ä¹ ï¼', obj.score);
var obj2 = new student(2, 'çäº', '女', '1970-01-01', 85);
console.log('å
¥åï¼', 2, 'çäº', '女', '1970-01-01', 85);
console.log('å¹´é¾ï¼', obj2.getAge());
obj2.study();
console.log('å¦ä¹ ï¼', obj2.score);
obj2.study();
console.log('å¦ä¹ ï¼', obj2.score);
obj2.study();
console.log('å¦ä¹ ï¼', obj2.score);
</script>
</body>
</html>