当前位置:首页 > JavaScript

js extends 实现

2026-02-02 08:35:37JavaScript

继承的实现方式

在JavaScript中,继承可以通过多种方式实现。以下是几种常见的继承实现方法:

原型链继承

原型链继承是通过将子类的原型对象指向父类的实例来实现的。子类通过原型链可以访问父类的属性和方法。

function Parent() {
    this.name = 'Parent';
}

Parent.prototype.sayName = function() {
    console.log(this.name);
};

function Child() {
    this.childName = 'Child';
}

Child.prototype = new Parent();

const child = new Child();
child.sayName(); // 输出: Parent

构造函数继承

构造函数继承是通过在子类的构造函数中调用父类的构造函数来实现的。这种方式可以避免引用类型的共享问题。

function Parent(name) {
    this.name = name;
}

function Child(name, age) {
    Parent.call(this, name);
    this.age = age;
}

const child = new Child('Child', 10);
console.log(child.name); // 输出: Child

组合继承

组合继承结合了原型链继承和构造函数继承的优点,既能够继承父类的原型方法,又能够避免引用类型的共享问题。

js extends 实现

function Parent(name) {
    this.name = name;
}

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;

const child = new Child('Child', 10);
child.sayName(); // 输出: Child

原型式继承

原型式继承是通过创建一个临时构造函数来实现的,适用于不需要单独创建构造函数的情况。

const parent = {
    name: 'Parent',
    sayName: function() {
        console.log(this.name);
    }
};

const child = Object.create(parent);
child.name = 'Child';
child.sayName(); // 输出: Child

寄生式继承

寄生式继承是在原型式继承的基础上增强对象,返回一个增强后的对象。

js extends 实现

function createChild(parent) {
    const child = Object.create(parent);
    child.sayAge = function() {
        console.log(this.age);
    };
    return child;
}

const parent = {
    name: 'Parent'
};

const child = createChild(parent);
child.age = 10;
child.sayAge(); // 输出: 10

寄生组合式继承

寄生组合式继承是组合继承的优化版本,避免了两次调用父类构造函数的问题。

function Parent(name) {
    this.name = name;
}

Parent.prototype.sayName = function() {
    console.log(this.name);
};

function Child(name, age) {
    Parent.call(this, name);
    this.age = age;
}

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

const child = new Child('Child', 10);
child.sayName(); // 输出: Child

ES6 Class继承

ES6引入了classextends关键字,使得继承更加简洁和直观。

class Parent {
    constructor(name) {
        this.name = name;
    }

    sayName() {
        console.log(this.name);
    }
}

class Child extends Parent {
    constructor(name, age) {
        super(name);
        this.age = age;
    }
}

const child = new Child('Child', 10);
child.sayName(); // 输出: Child

继承的选择

根据不同的需求选择合适的继承方式:

  • 如果需要简单的继承,可以使用原型链继承或ES6的classextends
  • 如果需要避免引用类型的共享问题,可以使用构造函数继承或组合继承。
  • 如果需要更灵活的继承方式,可以使用原型式继承或寄生式继承。
  • 如果需要最优化的继承方式,可以使用寄生组合式继承。

注意事项

  • 原型链继承会导致引用类型的属性被所有实例共享。
  • 构造函数继承无法继承父类原型上的方法。
  • 组合继承会调用两次父类构造函数,可能影响性能。
  • ES6的classextends语法糖背后仍然是基于原型的继承。

标签: jsextends
分享给朋友:

相关文章

js实现列表

js实现列表

使用 JavaScript 实现列表 JavaScript 提供了多种方式来实现列表功能,包括数组操作、DOM 元素动态生成等。以下是几种常见的实现方法: 使用数组存储列表数据 数组是 JavaS…

js轮播图实现原理

js轮播图实现原理

轮播图的基本原理 轮播图通过动态切换显示的内容(图片、文本等)实现视觉上的滑动效果。核心原理是利用CSS和JavaScript控制元素的显示与隐藏,或通过变换位置实现滑动动画。 实现方法一:…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组…

js实现瀑布流

js实现瀑布流

实现瀑布流布局 瀑布流布局是一种常见的网页布局方式,常用于图片展示、商品列表等场景。以下是使用 JavaScript 实现瀑布流布局的几种方法。 纯 JavaScript 实现 通过计算元素的位置…

js实现二叉树

js实现二叉树

二叉树的基本概念 二叉树是一种树形数据结构,每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的常见操作包括插入、删除、遍历等。 二叉树的节点定义 在JavaScript中,二叉树的节点可…