JavaScript 类基础教程文档

收录于 2023-04-20 00:10:05 · بالعربية · English · Español · हिंदीName · 日本語 · Русский язык · 中文繁體

在JavaScript中,类是特殊的函数类型。我们可以像函数声明和函数表达式一样定义类。
JavaScript类在主体中包含各种类成员,包括方法或构造函数。该类以严格模式执行。因此,包含静默错误或错误的代码会引发错误。
类语法包含两个组件:
类声明 类表达式

类声明

可以使用类声明来定义类。 class关键字用于声明具有任何特定名称的类。根据JavaScript的命名约定,类的名称始终以大写字母开头。

类声明示例

让我们看一个声明类的简单示例。
<script>
//Declaring class
class Employee
  {
//Initializing an object
    constructor(id,name)
    {
      this.id=id;
      this.name=name;
    }
//Declaring method
    detail()
    {
  document.writeln(this.id+" "+this.name+"<br>")
    }
  }
//passing object to a variable
var e1=new Employee(101,"Martin Roy");
var e2=new Employee(102,"Duke William");
e1.detail(); //calling method
e2.detail();
</script>
输出:
101 Martin Roy
102 Duke William

类声明示例-提升

与函数声明不同,类声明不是JavaScript提升的一部分。因此,需要在调用该类之前对其进行声明。
让我们看一个示例。
<script>
//Here, we are invoking the class before declaring it.
var e1=new Employee(101,"Martin Roy");
var e2=new Employee(102,"Duke William");
e1.detail(); //calling method
e2.detail();
//Declaring class
class Employee
  {
//Initializing an object
    constructor(id,name)
    {
      this.id=id;
      this.name=name;
    }
    detail()
    {
  document.writeln(this.id+" "+this.name+"<br>")
    }
  }
</script>
输出:
 JavaScript OOPs类

类声明示例:重复声明类

一个类只能声明一次。如果我们多次声明类,则会抛出错误。
让我们看一个例子。
<script>
//Declaring class
class Employee
  {
//Initializing an object
    constructor(id,name)
    {
      this.id=id;
      this.name=name;
    }
    detail()
    {
  document.writeln(this.id+" "+this.name+"<br>")
    }
  }
//passing object to a variable
var e1=new Employee(101,"Martin Roy");
var e2=new Employee(102,"Duke William");
e1.detail(); //calling method
e2.detail();
//Re-declaring class
class Employee
  {
  }
</script>
输出:
 JavaScript OOPs类

类表达式

定义类的另一种方法是使用类表达式。在此,不必强制分配类的名称。因此,可以对类表达式进行命名或未命名。类表达式允许我们获取类名称。但是,使用类声明是不可能的。

未命名的类表达式

无需指定任何名称即可表示该类。
让我们看一个例子。
<script>
var emp = class {
  constructor(id, name) {
    this.id = id;
    this.name = name;
  }
};
document.writeln(emp.name);
</script>
输出:
emp

类表达式示例:重复声明类

与类声明不同,类表达式允许我们重新声明相同的类。因此,如果我们尝试多次声明该类,则会引发错误。
<script>
//Declaring class
var emp=class
  {
//Initializing an object
    constructor(id,name)
    {
      this.id=id;
      this.name=name;
    }
//Declaring method
detail()
    {
  document.writeln(this.id+" "+this.name+"<br>")
    }
  }
//passing object to a variable
var e1=new emp(101,"Martin Roy");
var e2=new emp(102,"Duke William");
e1.detail(); //calling method
e2.detail();
//Re-declaring class
var emp=class
  {
//Initializing an object
    constructor(id,name)
    {
      this.id=id;
      this.name=name;
    }
    detail()
    {
  document.writeln(this.id+" "+this.name+"<br>")
    }
  }
//passing object to a variable
var e1=new emp(103,"James Bella");
var e2=new emp(104,"Nick Johnson");
e1.detail(); //calling method
e2.detail();
</script>
输出:
101 Martin Roy
102 Duke William
103 James Bella
104 Nick Johnson

命名类表达式示例

我们可以用特定名称表示类。在此,类名称的范围取决于类主体。使用class.name属性检索该类。
<script>
var emp = class Employee {
  constructor(id, name) {
    this.id = id;
    this.name = name;
  }
};
document.writeln(emp.name);
/*document.writeln(Employee.name);
Error occurs on console:
"ReferenceError: Employee is not defined
*/
</script>
输出:
Employee