JavaScript继承主要通过原型链实现,ES6前无class语法,ES6引入class/extends语法糖但底层仍基于原型链和构造函数,常见方式有原型链继承、构造函数继承、组合继承和ES6 class继承。

JavaScript 中的继承主要通过原型链实现,没有传统面向对象语言中的 class 继承语法(ES6 之前),但 ES6 引入了 class 和 extends 语法糖,底层仍是基于原型链和构造函数。常见实现方式有以下几种:
让子构造函数的 prototype 指向父构造函数的一个实例。
优点:简单,能复用父类方法;
缺点:所有子实例共享父类实例的引用属性,无法向父构造函数传参。
示例:
立即学习“Java免费学习笔记(深入)”;
function Animal(name) {
this.name = name;
this.colors = ['black', 'white'];
}
Animal.prototype.say = function() {
return `I'm ${this.name}`;
};
function Dog() {}
Dog.prototype = new Animal(); // 关键:继承原型
Dog.prototype.constructor = Dog;
const d1 = new Dog();
const d2 = new Dog();
d1.colors.push('brown');
console.log(d2.colors); // ['black', 'white', 'brown'] ← 共享了引用类型
在子构造函数中用 call 或 apply 调用父构造函数。
优点:避免引用属性共享,支持传参;
缺点:只能继承实例属性/方法,无法复用父类原型上的方法。
示例:
立即学习“Java免费学习笔记(深入)”;
function Animal(name) {
this.name = name;
this.colors = ['black', 'white'];
}
Animal.prototype.say = function() {
return `I'm ${this.name}`;
};
function Dog(name, breed) {
Animal.call(this, name); // 关键:继承实例属性
this.breed = breed;
}
const d = new Dog('Xiaohei', 'Shepherd');
console.log(d.name); // 'Xiaohei'
console.log(d.say); // undefined ← 原型方法没被继承
结合前两种:原型链继承方法 + 构造函数继承属性。
动态WEB网站中的PHP和MySQL详细反映实际程序的需求,仔细地探讨外部数据的验证(例如信用卡卡号的格式)、用户登录以及如何使用模板建立网页的标准外观。动态WEB网站中的PHP和MySQL的内容不仅仅是这些。书中还提到如何串联JavaScript与PHP让用户操作时更快、更方便。还有正确处理用户输入错误的方法,让网站看起来更专业。另外还引入大量来自PEAR外挂函数库的强大功能,对常用的、强大的包
525
优点:兼顾属性独立与方法复用;
缺点:父构造函数被调用两次(一次在设置原型时,一次在子构造函数内)。
示例:
立即学习“Java免费学习笔记(深入)”;
function Animal(name) {
this.name = name;
this.colors = ['black', 'white'];
}
Animal.prototype.say = function() {
return `I'm ${this.name}`;
};
function Dog(name, breed) {
Animal.call(this, name); // 第一次调用
this.breed = breed;
}
Dog.prototype = new Animal(); // 第二次调用 ← 可优化
Dog.prototype.constructor = Dog;
const d = new Dog('Wangcai', 'Poodle');
console.log(d.say()); // 'I'm Wangcai'
console.log(d.colors); // ['black', 'white'](不共享)
本质是组合继承的语法糖,内部自动处理原型和构造调用,更简洁安全。
关键点:
– 必须在子类 constructor 中调用 super();
– super 既可调用父类构造函数,也可访问父类原型方法;
– 支持 static 方法、get/set、new.target 等。
示例:
立即学习“Java免费学习笔记(深入)”;
class Animal {
constructor(name) {
this.name = name;
this.colors = ['black', 'white'];
}
say() {
return `I'm ${this.name}`;
}
static info() {
return 'Animal base class';
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name); // ← 必须先调用,初始化 this
this.breed = breed;
}
bark() {
return `${this.say()} and I bark!`;
}
}
const d = new Dog('Husky', 'Siberian');
console.log(d.bark()); // 'I'm Husky and I bark!'
console.log(Dog.info()); // 'Animal base class'
基本上就这些。日常开发优先用 class + extends,清晰、语义强、工具友好;理解原型链继承有助于调试和深入掌握 JS 机制。
以上就是javascript中的继承如何实现_有哪些方法?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号