<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
//Es6之后
//定义一个学生的类
class Student{
constructor(name){
this.name=name;
}
run(){
alert("run...");
}
}
class primaryStudent extends Student{
constructor(name,age,sex){
super(name);
this.age = age;
this.sex = sex;
}
hello(){
alert("hello!")
}
}
var zhangsan = new Student("zhangsan");
var lisi = new primaryStudent("lisi",15,"男");
</script>
</body>
</html>
Es6之后
可以通过构造器来构造属性
给对象添加方法也不用写function了
这篇博客介绍了ES6中如何使用类(Class)来定义对象,并通过构造器(constructor)设置属性。文章还展示了如何使用继承(extends)来创建子类,并在子类中添加新的方法。示例代码中创建了`Student`基类和`primaryStudent`子类,演示了类的实例化和方法调用。
758

被折叠的 条评论
为什么被折叠?



