了解 JavaScript 类 
JavaScript 类是其原型继承系统的语法糖。 ES6 中引入的类提供了一种清晰且结构化的方式来定义对象并使用 JavaScript 中的继承,使代码更具可读性和组织性。
定义一个类 
你可以使用 class 关键字定义一个类。
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 class  Person  {    constructor (name, age ) {         this .name  = name;         this .age  = age;     }     greet ( ) {         console .log (`我的名字是: ${this .name} ,我的年龄是:${this .age} ` );     } } const  person1 = new  Person ("webape" , 25 );person1.greet ();  
 
类的主要特点 
构造函数方法: 
构造函数是初始化新对象的特殊方法。当创建该类的新实例时,它会被自动调用。
例子:
1 2 3 4 5 6 7 8 class  Car  {    constructor (brand ) {         this .brand  = brand;     } } const  myCar = new  Car ("宝马" );console .log (myCar.brand ); 
 
方法: 
在类中定义的函数称为方法。
例子:
1 2 3 4 5 6 7 8 class  Animal  {    sound ( ) {         console .log ("这是测试信息" );     } } const  dog = new  Animal ();dog.sound ();  
 
静态方法: 
使用 static 关键字定义的方法属于类本身,而不是类的实例。
例子:
1 2 3 4 5 6 7 class  MathUtils  {    static  add (a, b ) {         return  a + b;     } } console .log (MathUtils .add (3 , 5 )); 
 
Getters 和 Setters: 
访问和修改属性的特殊方法。
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 class  Rectangle  {    constructor (width, height ) {         this .width  = width;         this .height  = height;     }     get  area () {         return  this .width  * this .height ;     } } const  rect = new  Rectangle (10 , 5 );console .log (rect.area ); 
 
类中的继承 
继承允许一个类使用 extends 关键字从另一个类获取属性和方法。
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class  Animal  {    constructor (name ) {         this .name  = name;     }     speak ( ) {         console .log (`${this .name}  叫` );     } } class  Dog  extends  Animal  {    speak ( ) {         console .log (`${this .name}  吠.` );     } } const  dog = new  Dog ("Rex" );dog.speak ();  
 
私有字段和方法 
ES2022 中引入的私有字段和方法以 # 开头,不能在类外部访问。
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class  BankAccount  {    #balance;     constructor (initialBalance ) {         this .#balance = initialBalance;     }     deposit (amount ) {         this .#balance += amount;         console .log (`存入: ${amount} ` );     }     getBalance ( ) {         return  this .#balance;     } } const  account = new  BankAccount (100 );account.deposit (50 );  console .log (account.getBalance ()); 
 
类表达式 
类也可以定义为表达式并分配给变量。
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 const  Rectangle  = class  {    constructor (width, height ) {         this .width  = width;         this .height  = height;     }     getArea ( ) {         return  this .width  * this .height ;     } }; const  rect = new  Rectangle (10 , 5 );console .log (rect.getArea ()); 
 
与原型混合 
虽然类是语法糖,但你仍然可以将它们与 JavaScript 基于原型的继承结合起来。
例子:
1 2 3 4 5 6 7 class  Person  {}Person .prototype  .sayHello  = function  ( ) {    console .log ("Hello!" ); }; const  person = new  Person ();person.sayHello ();  
 
类的最佳实践 
使用私有字段来保护敏感数据。
利用继承在多个类之间重用代码。
仅在必要时使用类。简单的对象或函数可能足以完成小任务。
遵循方法和属性的命名约定以提高可读性。
总结 
JavaScript 类提供了一种干净有效的方法来管理 JavaScript 中的面向对象编程。凭借继承、静态方法、私有字段和封装等功能,它们提供了强大的工具来构建和管理代码,从而更轻松地构建可扩展和可维护的应用程序。
      
     
    
      
  
 
  
    
      
      
        
        致力于网站建设与Web开发。喜欢新事物,关注前后端动态,对新的技术有追求, 做一个优秀的web全栈工程师。