构造函数

  1. class Person{
  2. constructor(name,age){
  3. this.name=name;
  4. this.age=age;
  5. }
  6. sayHi(){
  7. console.log(`hello,${this.name},今年我${this.age}了`)
  8. }
  9. }

class类的写法等价于函数写法

  1. function Person(name,age){
  2. this.name=name;
  3. this.age=age
  4. }
  5. Person.prototype.sayHi=function(){
  6. console.log(`hello,${this.name},今年我${this.age}了`)
  7. }
  8. var p = new Person('dong',18)

静态方法

  1. class EventCenter {
  2. static fire() {
  3. return 'fire';
  4. }
  5. static on(){
  6. return 'on
  7. }
  8. }

继承

  1. class Person{
  2. constructor(name,age){
  3. this.name=name;
  4. this.age=age;
  5. }
  6. sayHi(){
  7. console.log(`hello,${this.name},今年我${this.age}了`)
  8. }
  9. }
  10. class Student extends Person{
  11. constructor(name,age,score){
  12. super(name,age);
  13. this.score = score;
  14. }
  15. myScore(){
  16. console.log(`hello,${this.name},我考了${this.score}分`)
  17. }
  18. }