Skip to content

类

一句话解释

类 是 blueprint for 创建 对象 使用 predefined 属性 and 方法, introduced in ES6 as syntactic sugar over 原型.

用大白话说

class 是一种template for creating objects。 It defines what properties and methods all instances of the class will have. ES6 class syntax makes object-oriented code in JavaScript look more like class-based languages, but under the hood it still uses prototypal inheritance.

A class has a constructor method that runs when you create a new instance with 新. Methods defined in the class body are added to the prototype. You can extend classes with extends and override methods in subclasses.

Classes support getters, setters, static methods, and private fields (with # prefix in modern JavaScript). However, JavaScript classes are still fundamentally prototypes — the class keyword is syntactic convenience.

示例

javascript
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  greet() { return `Hi, I'm ${this.name}`; }
}
const ada = new Person("Ada", 36);
console.log(ada.greet()); // "Hi, I'm Ada"

类 使用 constructor and 方法. 新 创建 实例.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表