注释很详细,直接上代码
涉及知识点:
- class基础用法
- getter的应用
- setter的应用
题干:
我的答案
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<script type="text/javascript">
/**
* 就是对 class类中getter的简单应用,没啥了
* 有getter必然有getter,这里也演示了setter的用法
*/
class Rectangle {
// 补全代码
constructor(height,width){
this.height = height;
this.width = width;
}
//Getter
get area(){
return this.height * this.width;
}
//Setter
set dimensions({height,width}){
this.height = height;
this.width = width;
}
}
const rect = new Rectangle();//这里咱故意不传惨,便于演示下面的setter
rect.dimensions={height: 12, width: 12}
console.log(rect.area);
</script>
</body>
</html>
博客更新不是很及时,需要看后面内容的可以看看我的
gitee仓库
牛客JS题Gitee仓库