js中的继承方式有多种,原型链继承通过子类型原型指向父类型实例实现,优点是实现简单且方法可复用,缺点是所有实例共享引用类型属性且无法向父类构造函数传参;构造函数继承通过在子类构造函数中调用父类构造函数解决属性共享问题,优点是可传递参数且属性独立,缺点是无法继承父类原型方法且方法不可复用;组合继承结合前两者优点,通过原型链继承原型方法、构造函数继承实例属性,实现了方法复用和属性独立,但父类构造函数被调用两次导致性能浪费;原型式继承基于现有对象创建新对象,适用于无需自定义类型的场景,但存在引用属性共享问题;寄生式继承在原型式继承基础上增强对象,适合临时扩展对象功能,但每次都会创建新方法;寄生组合式继承通过继承父类原型的副本来避免构造函数重复调用,解决了组合继承的性能问题,被认为是目前最理想的继承方式。选择继承方式应根据具体需求权衡。

JS中的继承,简单来说,就是让一个对象拥有另一个对象的属性和方法。实现方式有很多,各有优劣,选择哪种取决于具体需求。

解决方案:
实现JS继承的方法主要有以下几种:原型链继承、构造函数继承(也叫经典继承)、组合继承(原型链继承 + 构造函数继承)、原型式继承、寄生式继承、寄生组合式继承。

原型链继承的核心在于利用原型对象,子类型的原型指向父类型的一个实例。这样,子类型就可以访问父类型原型上的属性和方法。
优点是实现简单,父类方法可以复用。

缺点也很明显:
function Parent() {
this.name = 'Parent';
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.sayName = function() {
console.log(this.name);
}
function Child() {
this.childName = 'Child';
}
Child.prototype = new Parent(); // 关键:让Child的原型指向Parent的实例
Child.prototype.constructor = Child; // 修正constructor指向
let child1 = new Child();
child1.colors.push('black');
let child2 = new Child();
console.log(child2.colors); // ["red", "blue", "green", "black"] child2也受到了影响构造函数继承,也叫经典继承,通过在子类型的构造函数中调用父类型的构造函数来实现。
优点:
缺点:
function Parent(name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
this.sayName = function() { // 每次new Parent都会重新创建
console.log(this.name);
}
}
function Child(name) {
Parent.call(this, name); // 关键:在子类型构造函数中调用父类型构造函数
this.childName = 'Child';
}
let child1 = new Child('Child1');
child1.colors.push('black');
let child2 = new Child('Child2');
console.log(child2.colors); // ["red", "blue", "green"] child2没有受到影响组合继承结合了原型链继承和构造函数继承的优点,避免了它们的缺点。
思路是:使用原型链继承来实现对父类型原型属性和方法的继承,使用构造函数继承来实现实例属性的继承。
本书是全面讲述PHP与MySQL的经典之作,书中不但全面介绍了两种技术的核心特性,还讲解了如何高效地结合这两种技术构建健壮的数据驱动的应用程序。本书涵盖了两种技术新版本中出现的最新特性,书中大量实际的示例和深入的分析均来自于作者在这方面多年的专业经验,可用于解决开发者在实际中所面临的各种挑战。
466
优点:
缺点:
function Parent(name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.sayName = function() {
console.log(this.name);
}
function Child(name, age) {
Parent.call(this, name); // 构造函数继承
this.age = age;
}
Child.prototype = new Parent(); // 原型链继承
Child.prototype.constructor = Child; // 修正constructor指向
let child1 = new Child('Child1', 10);
child1.colors.push('black');
let child2 = new Child('Child2', 12);
console.log(child2.colors); // ["red", "blue", "green"]
child1.sayName(); // Child1
child2.sayName(); // Child2原型式继承,不用创建构造函数,直接基于现有对象创建一个新对象。Object.create() 就是原型式继承的实现。
let person = {
name: 'Person',
friends: ['Shelby', 'Court', 'Van']
};
let anotherPerson = Object.create(person);
anotherPerson.name = 'Greg';
anotherPerson.friends.push('Rob');
let yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = 'Linda';
yetAnotherPerson.friends.push('Barbie');
console.log(person.friends); // [ 'Shelby', 'Court', 'Van', 'Rob', 'Barbie' ]原型式继承的缺点和原型链继承类似,修改一个实例的引用类型属性,会影响到其他实例。
寄生式继承,在原型式继承的基础上,增强对象,返回一个新对象。
function createAnother(original) {
let clone = Object.create(original); // 通过调用函数创建一个新对象
clone.sayHi = function() { // 以某种方式增强这个对象
console.log('hi');
}
return clone; // 返回这个对象
}
let person = {
name: 'Person',
friends: ['Shelby', 'Court', 'Van']
};
let anotherPerson = createAnother(person);
anotherPerson.sayHi(); // "hi"寄生式继承的缺点是,每个对象都会创建一遍方法。
原型式继承和寄生式继承,主要用于不需要自定义类型,只是想得到一个对象的副本,并对其进行一些修改的场景。
寄生组合式继承,被认为是目前最理想的继承方式。它解决了组合继承中父类构造函数被调用两次的问题。
核心思想是:不通过调用父类构造函数来给子类原型赋值,而是通过创建父类原型的副本来实现。
function inheritPrototype(child, parent) {
let prototype = Object.create(parent.prototype); // 创建父类原型的副本
prototype.constructor = child; // 修正constructor指向
child.prototype = prototype; // 将副本赋值给子类原型
}
function Parent(name) {
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.sayName = function() {
console.log(this.name);
}
function Child(name, age) {
Parent.call(this, name);
this.age = age;
}
inheritPrototype(Child, Parent);
let child1 = new Child('Child1', 10);
child1.colors.push('black');
let child2 = new Child('Child2', 12);
console.log(child2.colors); // ["red", "blue", "green"]
child1.sayName(); // Child1
child2.sayName(); // Child2inheritPrototype 函数起到了关键作用,它避免了调用 Parent 构造函数,只使用了 Parent.prototype 的副本。
选择哪种继承方式,取决于你的具体需求。 如果只是简单的想复用父类原型上的方法,原型链继承可以考虑。 如果需要传递参数,并且不关心方法复用,构造函数继承可以考虑。 如果追求更完善的继承方式,寄生组合式继承是更好的选择。
以上就是js中如何实现继承的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号