Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
掌握 JavaScript 类:现代 OOP 完整指南_创想鸟

掌握 JavaScript 类:现代 OOP 完整指南

掌握 javascript 类:现代 oop 完整指南

JavaScript 类:现代面向对象编程

ES6 引入的 JavaScript 类,是基于原型继承的语法糖衣,提供了一种更清晰、结构化的方法来定义和使用对象以及继承机制,从而提升代码的可读性和组织性。

类定义

使用 class 关键字定义类:

示例:

class Person {  constructor(name, age) {    this.name = name;    this.age = age;  }  greet() {    console.log(`您好,我的名字是 ${this.name},我 ${this.age} 岁。`);  }}const person1 = new Person("Alice", 25);person1.greet(); // 您好,我的名字是 Alice,我 25 岁。

核心特性

构造函数 (constructor): 用于初始化对象实例的特殊方法,在创建新实例时自动调用。

立即学习“Java免费学习笔记(深入)”;

示例:

class Car {  constructor(brand) {    this.brand = brand;  }}const myCar = new Car("Toyota");console.log(myCar.brand); // Toyota

方法 (Methods): 类内部定义的函数。

示例:

class Animal {  makeSound() {    console.log("动物发出声音");  }}const dog = new Animal();dog.makeSound(); // 动物发出声音

静态方法 (Static Methods): 使用 static 关键字定义,属于类本身,而非实例。

示例:

class MathUtils {  static add(a, b) {    return a + b;  }}console.log(MathUtils.add(3, 5)); // 8

Getter 和 Setter: 用于访问和修改属性的特殊方法。

示例:

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); // 50

类继承

使用 extends 关键字实现继承:

示例:

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(); // Rex 汪汪叫。

私有字段和方法 (ES2022)

以 # 开头,只能在类内部访问。

示例:

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); // 存款:50console.log(account.getBalance()); // 150// console.log(account.#balance); // Error: 私有字段 '#balance' 无法访问

类表达式

类可以定义为表达式并赋值给变量:

示例:

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()); // 50

原型继承与类的结合

类可以与 JavaScript 的原型继承结合使用。

示例:

class Person {}Person.prototype.sayHello = function() {  console.log("你好!");};const person = new Person();person.sayHello(); // 你好!

最佳实践

封装: 使用私有字段保护数据。可重用性: 利用继承重用代码。避免过度复杂: 适度使用类。一致性: 遵循命名规范。

总结

JavaScript 类提供了一种更清晰、高效的面向对象编程方式,通过继承、静态方法、私有字段和封装等特性,方便构建可扩展、易维护的应用程序。

作者:Abhay Singh Kathayat
全栈开发人员,精通多种编程语言和框架。联系邮箱:kaashshorts28@gmail.com

以上就是掌握 JavaScript 类:现代 OOP 完整指南的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1499630.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
了解 JavaScript 原型:继承和方法共享综合指南
上一篇 2025年12月19日 21:55:43
掌握 XMLHttpRequest:JavaScript 中 AJAX 调用指南
下一篇 2025年12月19日 21:56:07

相关推荐

发表回复

登录后才能评论
关注微信