返回
深入浅出ES6+设计模式解析(开山篇:面向对象复习篇)
前端
2024-01-21 12:11:16
在学习设计模式之前,对于初学者而言,面向对象是需要重温巩固一下的知识。这里将以ES6+语法来对面向对象进行介绍。
1. 类和对象
类是一种模板,用于创建具有相似特征和行为的对象。对象是类的实例,可以访问类的属性和方法。
// 定义一个类
class Person {
constructor(name, age) {
// 属性
this.name = name;
this.age = age;
}
// 方法
speak() {
console.log(`My name is ${this.name}, and I am ${this.age} years old.`);
}
}
// 创建一个对象
const person1 = new Person('John', 30);
// 调用对象的方法
person1.speak(); // My name is John, and I am 30 years old.
2. 构造函数
构造函数是类的特殊方法,用于在创建对象时初始化对象属性。构造函数的名称必须与类的名称相同。
// 定义一个类
class Person {
constructor(name, age) {
// 在这里初始化对象属性
this.name = name;
this.age = age;
}
// 其他方法
}
// 创建一个对象
const person1 = new Person('John', 30);
3. 原型
原型是类的实例共享的对象。原型包含类的所有属性和方法。当对象访问不存在的属性或方法时,JavaScript会在原型中查找该属性或方法。
// 定义一个类
class Person {
constructor(name, age) {
// 在这里初始化对象属性
this.name = name;
this.age = age;
}
// 方法
speak() {
console.log(`My name is ${this.name}, and I am ${this.age} years old.`);
}
}
// 获取类的原型
const personPrototype = Person.prototype;
// 向原型添加一个属性
personPrototype.gender = 'male';
// 创建一个对象
const person1 = new Person('John', 30);
// 访问原型属性
console.log(person1.gender); // male
4. 继承
继承允许一个类从另一个类继承属性和方法。派生类可以重写继承自基类的属性和方法。
// 定义一个基类
class Person {
constructor(name, age) {
// 在这里初始化对象属性
this.name = name;
this.age = age;
}
// 方法
speak() {
console.log(`My name is ${this.name}, and I am ${this.age} years old.`);
}
}
// 定义一个派生类
class Student extends Person {
constructor(name, age, major) {
// 调用基类的构造函数
super(name, age);
// 初始化派生类的属性
this.major = major;
}
// 重写基类的方法
speak() {
console.log(`My name is ${this.name}, and I am ${this.age} years old. I am a student majoring in ${this.major}.`);
}
}
// 创建一个派生类对象
const student1 = new Student('John', 30, 'Computer Science');
// 调用派生类的方法
student1.speak(); // My name is John, and I am 30 years old. I am a student majoring in Computer Science.
5. 封装
封装是将数据和操作数据的方法绑定在一起的一种机制。封装可以防止外部代码直接访问对象的数据,从而保护数据的完整性和安全性。
// 定义一个类
class Person {
constructor(name, age) {
// 私有属性
const _name = name;
const _age = age;
// 公共方法
this.getName = function() {
return _name;
};
this.getAge = function() {
return _age;
};
}
}
// 创建一个对象
const person1 = new Person('John', 30);
// 尝试直接访问私有属性
console.log(person1._name); // undefined
console.log(person1._age); // undefined
// 通过公共方法访问私有属性
console.log(person1.getName()); // John
console.log(person1.getAge()); // 30
6. 多态
多态允许对象以不同的方式响应相同的消息。多态性是通过继承和方法重写来实现的。
// 定义一个基类
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`My name is ${this.name}.`);
}
}
// 定义一个派生类
class Dog extends Animal {
constructor(name) {
super(name);
}
speak() {
console.log(`My name is ${this.name}. Woof!`);
}
}
// 定义另一个派生类
class Cat extends Animal {
constructor(name) {
super(name);
}
speak() {
console.log(`My name is ${this.name}. Meow!`);
}
}
// 创建一个基类对象
const animal1 = new Animal('Animal');
// 创建一个派生类对象
const dog1 = new Dog('Dog');
const cat1 = new Cat('Cat');
// 调用基类和派生类的方法
animal1.speak(); // My name is Animal.
dog1.speak(); // My name is Dog. Woof!
cat1.speak(); // My name is Cat. Meow!
7. 总结
面向对象是JavaScript中一种重要的编程范式。通过类、对象、构造函数、原型、继承、封装和多态,我们可以创建出具有复杂行为和功能的程序。