h5网站开发 源码,自行建网站 所需费用,移动开发和网站开发,国家企业信用信息公示系统江苏ES6提供了更接近传统语言的写法#xff0c;引入了Class类这个概念#xff0c;作为对象的模板。通过Class关键字#xff0c;可以定义类#xff0c;基本上#xff0c;ES6的class可以看作只是一个语法糖#xff0c;它的绝大部分功能#xff0c;ES5都可以做到#xff0c;新…ES6提供了更接近传统语言的写法引入了Class类这个概念作为对象的模板。通过Class关键字可以定义类基本上ES6的class可以看作只是一个语法糖它的绝大部分功能ES5都可以做到新的class写法只是让对象原型的写法更加清晰、更面向对象编程的语法而已。
一、class类的基本用法
基本语法 class 类名 { constructor{ } } class Student {// 构造方法 名字不能修改constructor(name,age) {this.name namethis.age age}// 添加方法// 方法必须使用该语法fun() {console.log(我是学生)}}let zs new Student(张三,18)console.log(zs) 二、class类静态成员
static class Student {// 静态属性static name 张三static fun() {console.log(我是学生)}}let zs new Student()console.log(zs.name) //undefinedconsole.log(Student.name) //张三为什么我们zs.name打印undefined呢 因为static属性方法只属于类不属于实例对象
三、class类继承 class Student {// 构造方法 名字不能修改constructor(name, age) {this.name namethis.age age}// 添加方法// 方法必须使用该语法fun() {console.log(我是学生)}}// 类继承必须要写extendsclass Student1 extends Student {// 构造方法constructor(name,age,id,tel){// supersuper(name,age) //Student.call(this,name,age)this.id idthis.tel tel}learn() {console.log(学习)}}let zs new Student1(张三,18,10,123456)console.log(zs)四、class中的getter与setter class Student {get name(){console.log(姓名被读取了);// 需要有返回值 要不然直接 .上name会输出undefinedreturn hihi}// set 中需要有参数set name(value){console.log(姓名被修改了);}}let s new Student()console.log(s.name);s.name say感谢大家的阅读如有不对的地方可以向我提出感谢大家