参考示例
<table id="roster"></table>
<script>
const rootElement = document.getElementById('roster');
class Student {
constructor(name, gender, score) {
this.name = name;
this.gender = gender;
this.score = score;
this.quality = 100;
this.mount();
}
/*
* 计算总分
* */
sumScore() {
return this.score + this.quality;
}
/**
* 将数据转换为HTML并插入到表格中
*/
mount() {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${this.name}</td>
<td>${this.gender}</td>
<td>${this.score}</td>
<td>${this.quality}</td>
<td>${this.sumScore()}</td>
`;
;
rootElement.appendChild(tr);
}
}
const whh = new Student('王花花', '男', 98);
const lsd = new Student('李拴蛋', '女', 50);
</script>
登录后评论