当前位置:首页 > JavaScript

实现继承js

2026-01-15 15:25:04JavaScript

实现继承的方式

在JavaScript中,实现继承可以通过多种方式完成,每种方式都有其优缺点。以下是几种常见的实现继承的方法:

原型链继承

原型链继承是最基本的继承方式,通过将子类的原型指向父类的实例来实现继承。

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

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

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

Child.prototype = new Parent();
Child.prototype.constructor = Child;

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

构造函数继承

构造函数继承通过在子类的构造函数中调用父类的构造函数来实现继承。

实现继承js

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;
}

const child = new Child('Child', 10);
console.log(child.name); // 输出: Child
console.log(child.age); // 输出: 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 = new Parent();
Child.prototype.constructor = Child;

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

原型式继承

原型式继承通过创建一个临时构造函数来实现继承,类似于Object.create()方法。

实现继承js

function createObject(obj) {
    function F() {}
    F.prototype = obj;
    return new F();
}

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

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

寄生式继承

寄生式继承在原型式继承的基础上增强了对象的功能。

function createEnhancedObject(obj) {
    const clone = Object.create(obj);
    clone.sayHello = function() {
        console.log('Hello');
    };
    return clone;
}

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

const child = createEnhancedObject(parent);
child.sayHello(); // 输出: Hello
child.sayName(); // 输出: Parent

寄生组合式继承

寄生组合式继承是目前最理想的继承方式,避免了组合继承中调用两次父类构造函数的问题。

function inheritPrototype(child, parent) {
    const prototype = Object.create(parent.prototype);
    prototype.constructor = child;
    child.prototype = prototype;
}

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;
}

inheritPrototype(Child, Parent);

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

ES6中的类继承

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

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
console.log(child.age); // 输出: 10

总结

JavaScript中实现继承的方式多种多样,从早期的原型链继承到现代的ES6类继承,每种方式都有其适用场景。选择哪种方式取决于具体的需求和项目环境。

标签: js
分享给朋友:

相关文章

js实现全屏

js实现全屏

实现全屏的基本方法 使用JavaScript实现全屏功能主要依赖Element.requestFullscreen()方法。现代浏览器均支持此API,但不同浏览器可能需要添加前缀。 // 触发全屏…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

js实现乘

js实现乘

JavaScript 实现乘法运算 在 JavaScript 中实现乘法运算可以通过多种方式完成,包括基本运算符、函数封装以及高级算法(如大数乘法)。以下是几种常见方法: 使用基本乘法运算符 Jav…

js实现论坛

js实现论坛

实现论坛的基本功能 使用JavaScript实现一个论坛需要结合前端和后端技术。前端可以使用React、Vue或Angular等框架,后端可以选择Node.js配合Express或Koa框架。数据库可…

js手势实现

js手势实现

手势识别实现方法 在JavaScript中实现手势识别通常涉及监听触摸事件(touchstart、touchmove、touchend)或鼠标事件(mousedown、mousemove、mouseu…

链表实现js

链表实现js

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和一个指向下一个节点的指针(单向链表)或两个指针(双向链表)。与数组不同,链表在内存中非连续存储,插入和删除操作更高效,但随机…