1、构造器模式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>构造器模式</title>
</head>
<body>
<script>
/*
* es5
*/
function Student1(name, gender, score) {
this.name = name;
this.gender = gender;
this.score = score;
this.qulity = 100;
this.sumScore = function () {
return this.score + this.qulity;
}
}
var whh = new Student1('王花花', '男', 89)
console.log(whh);
console.log(whh.sumScore());
</script>
<script>
/*
* es6
*/
class Student2 {
constructor(name, gender, score) {
this.name = name;
this.gender = gender;
this.score = score;
this.qulity = 100;
}
sumScore() {
return this.score + this.qulity;
}
}
var lsd = new Student2('李栓蛋', '女', 99)
console.log(lsd);
console.log(lsd.sumScore());
</script>
</body>
</html>