es6:在zuoye2.js文件定义Student类,包含学号,姓名,性别,班级属性和学习(study)方法,并默认导出该类,在zuoye2.html文件中导入该类,创建对象并调用其中的study方法。
时间: 2023-07-15 16:15:10 浏览: 62
在zuoye2.js文件中,定义一个Student类,如下所示:
```
export default class Student {
constructor(id, name, gender, cls) {
this.id = id;
this.name = name;
this.gender = gender;
this.cls = cls;
}
study() {
console.log(`${this.name} is studying in ${this.cls}.`);
}
}
```
在zuoye2.html文件中,导入该类并创建对象并调用其中的study方法,如下所示:
```
<!DOCTYPE html>
<html>
<head>
<title>ES6 Class Demo</title>
<script type="module">
import Student from './zuoye2.js';
const student = new Student('001', 'Tom', 'male', 'Class 1');
student.study();
</script>
</head>
<body>
<h1>ES6 Class Demo</h1>
</body>
</html>
```
在浏览器中打开该HTML文件,可以看到控制台输出`Tom is studying in Class 1.`,说明类和方法都已成功定义和使用。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)