Javascript可以实现继承的方法有很多?下面web建站小编给大家简单介绍一下!
原型链继承:利用原型让一个引用类型继承另一个引用类型的属性和方法
复制代码function Parent() { ... }
Parent.prototype.method = function() { ... }
function Child() { ... }
Child.prototype = new Parent(); // 继承Parent
let child = new Child();
child.method(); // 可以调用Parent的方法
- 1
- 2
- 3
- 4
- 5
- 6
- 7
组合继承:原型链继承与构造函数继承的组合
复制代码//使用原型链实现对父类方法的继承,使用构造函数实现对父类属性的继承。
function Parent() { ... }
Parent.prototype.method = function() { ... }
function Child() {
Parent.call(this); // 继承属性
}
Child.prototype = new Parent(); // 继承方法
Child.prototype.constructor = Child;
let child = new Child();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
构造函数继承:在子类构造函数中调用父类构造函数
复制代码function Parent() { ... }
function Child() {
Parent.call(this); // 继承Parent
}
let child = new Child();
- 1
- 2
- 3
- 4
- 5
原型式继承:使用一个空对象作为中介,继承另一个对象的属性
复制代码let parent = { ... };
let child = Object.create(parent); // 创建对象,parent为原型
child.method = function() { ... }
- 1
- 2
- 3
ES6 的 class 继承:使用 extends 关键字实现继承
复制代码class Parent { ... }
class Child extends Parent {
constructor() { ... }
}
let child = new Child();
- 1
- 2
- 3
- 4
- 5
javascript根据相同id合并形成child子数组(支持低版本浏览器)
标签: JavaScript方法, Javascript继承, js原型式继承, js原型链继承, js构造函数继承, js组合继承
上面是“Javascript有哪些方法可以实现继承”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_5076.html
workflows工作流
一个20岁丰满的女孩ComfyUI工作流
一只穿着黑色蝴蝶结西装可爱橙色小猫
一只可爱的毛茸茸的猫ComfyUI工作流
一个外国人在吹奏萨克斯ComfyUI工作流
一把令人难忘的美丽吉他ComfyUI工作流
一辆在泥潭中奔跑的布加迪ComfyUI工作流
一桌精致的美食,桌上几杯白葡萄酒
一个精心制作的微型赛车场ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!