Skip to content

脚本内容,实例的方法。

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  greet() {
    return `你好,我是${this.name},今年 ${this.age} 岁。`;
  }

  growOlder() {
    this.age ++;
    return `${this.name}现在 ${this.age} 岁了。`;
  }
}

const p1 = new Person('刘强', 25);
console.log(p1.greet());
console.log(p1.growOlder());
你好,我是刘强,今年 25 岁。
刘强现在 26 岁了。

脚本内容,类的静态方法。

class MathUtils {
  static add(a, b) {
    return a + b;
  }

}

console.log(MathUtils.add(3, 4));
7

联系 math@baima.site