JS个人学习(9)——JS常见的六种继承方式
2023-12-12 07:53:06
前言
JavaScript是一种面向对象的编程语言,继承是面向对象编程中的一项重要特性。继承允许我们创建新对象,并从现有的对象继承属性和方法。这使得我们可以复用代码,并创建更复杂的程序。
JavaScript中的六种继承方式
在JavaScript中,有六种常见的继承方式:
- 原型链继承
- 构造函数继承
- 组合继承
- 寄生继承
- 寄生组合继承
- ES6继承
原型链继承
原型链继承是JavaScript中最基本的一种继承方式。它利用了JavaScript中对象的原型链机制。每个对象都有一个原型对象,原型对象又可能有自己的原型对象,如此一层一层向上追溯,直到到达Object对象。
当我们创建新对象时,这个新对象会自动继承其原型对象的所有属性和方法。例如:
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
console.log(`Hello, my name is ${this.name}`);
};
const person = new Person('John');
person.sayHello(); // Hello, my name is John
在这个例子中,Person
函数是一个构造函数,它创建了一个名为person
的新对象。person
对象继承了Person.prototype
对象的所有属性和方法,因此它可以调用sayHello
方法。
构造函数继承
构造函数继承是另一种常见的继承方式。它通过在子类的构造函数中调用父类的构造函数来实现继承。例如:
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
console.log(`Hello, my name is ${this.name}`);
};
function Student(name, major) {
Person.call(this, name);
this.major = major;
}
Student.prototype = Object.create(Person.prototype);
const student = new Student('John', 'Computer Science');
student.sayHello(); // Hello, my name is John
在这个例子中,Student
函数是一个构造函数,它继承了Person
函数的属性和方法。在Student
函数的构造函数中,我们调用了Person
函数的构造函数,并将this
作为参数传递给它。这使得Student
对象可以继承Person
对象的所有属性和方法。
组合继承
组合继承是构造函数继承和原型链继承的结合。它通过在子类的构造函数中调用父类的构造函数,并在子类的原型对象中设置父类的原型对象来实现继承。例如:
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
console.log(`Hello, my name is ${this.name}`);
};
function Student(name, major) {
Person.call(this, name);
this.major = major;
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;
const student = new Student('John', 'Computer Science');
student.sayHello(); // Hello, my name is John
在这个例子中,Student
函数继承了Person
函数的属性和方法。在Student
函数的构造函数中,我们调用了Person
函数的构造函数,并将this
关键字作为参数传递给它。这使得Student
对象可以继承Person
对象的所有属性和方法。在Student
函数的原型对象中,我们设置了Person
函数的原型对象,这使得Student
对象可以访问Person
函数的原型对象中的属性和方法。
寄生继承
寄生继承是一种不使用构造函数的继承方式。它通过创建一个新对象,并将父对象的所有属性和方法复制到新对象中来实现继承。例如:
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
console.log(`Hello, my name is ${this.name}`);
};
const student = Object.create(Person.prototype);
student.name = 'John';
student.major = 'Computer Science';
student.sayHello(); // Hello, my name is John
在这个例子中,student
对象是一个新对象,它继承了Person
函数的原型对象的所有属性和方法。我们通过调用Object.create()
方法,并将Person.prototype
对象作为参数传递给它来创建student
对象。这使得student
对象可以访问Person
函数的原型对象中的属性和方法。
寄生组合继承
寄生组合继承是寄生继承和构造函数继承的结合。它通过在子类的原型对象中设置父类的原型对象,并在子类的构造函数中调用父类的构造函数来实现继承。例如:
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
console.log(`Hello, my name is ${this.name}`);
};
function Student(name, major) {
Person.call(this, name);
this.major = major;
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;
const student = new Student('John', 'Computer Science');
student.sayHello(); // Hello, my name is John
在这个例子中,Student
函数继承了Person
函数的属性和方法。在Student
函数的原型对象中,我们设置了Person
函数的原型对象,这使得Student
对象可以访问Person
函数的原型对象中的属性和方法。在Student
函数的构造函数中,我们调用了Person
函数的构造函数,并将this
关键字作为参数传递给它。这使得Student
对象可以继承Person
对象的所有属性和方法。
ES6继承
ES6中引入了新的继承语法,它使用extends
关键字来实现继承。例如:
class Person {
constructor(name) {
this.name = name;
}
sayHello() {
console.log(`Hello, my name is ${this.name}`);
}
}
class Student extends Person {
constructor(name, major) {
super(name);
this.major = major;
}
}
const student = new Student('John', 'Computer Science');
student.sayHello(); // Hello, my name is John
在这个例子中,Student
类继承了Person
类。在Student
类的构造函数中,我们调用了super()
方法,并将name
参数作为参数传递给它。这使得Student
对象可以继承Person
对象的所有属性和方法。
学习这块知识时所需要具备的前置知识条件
在学习JavaScript的继承之前,你需要具备以下前置知识条件:
- JavaScript的基本语法
- 对象和数组
- 函数
- 原型和原型链
如果你已经掌握了这些知识,那么你就可以开始学习JavaScript的继承了。
结语
继承是JavaScript中的一项重要特性,它允许我们复用代码,并创建更复杂的程序。JavaScript提供了六种常见的继承方式,包括原型链继承、构造函数继承、组合继承、寄生继承、寄生组合继承和ES6继承。每种方式都有各自的优缺点,在不同的场景下适用不同。