美文网首页
js常见的继承方式

js常见的继承方式

作者: 开着五菱宏光的小白 | 来源:发表于2021-09-08 15:50 被阅读0次

js常见的继承方式

1. 原型链继承

function Parent1() {
    this.name = 'parent1';
    this.play = [1,2,3];
}
function Child1() {
    this.type = 'child2'
}
Child1.prototype = new Parent1()
const child1 = new Child1();
const child2 = new Child1();
child1.play.push(3)
原型链继承

2. 构造函数继承

function Parent1() {
    this.name = 'parent1'
}
Parent1.prototype.getName = function() {
    return this.name
}
function Child1() {
    Parent1.call(this)
    this.type = 'child1'
}
const child1 = new Child1()
console.log(child1);
console.log(child1.getName);
构造函数继承

3. 组合式继承

function Parent1() {
    this.name = 'parent1';
    this.type = [1,2,3]
}
Parent1.prototype.say = function() {
    return this.attr
}
function Child1() {
    // 第二次执行
    Parent1.call(this)
    this.attr = 'child1';
}
// 第一次执行
Child1.prototype = new Parent1();
/* 修改构造函数指向 */
Child1.prototype.constructor = Child1;
const child1 = new Child1();
const child2 = new Child1();
child1.type.push(1)
console.log(child1);
console.log(child2);
console.log(child1.say());
console.log(child2.say());

组合式继承

4. 原型式继承

const Parent1 = {
    name: '张三',
    list: [1,2,3],
    getName: function() {
        return this.name
    }
}
const child1 = Object.create(Parent1)
const child2 = Object.create(Parent1)
child1.list.push(1)
child1.name = '亚索'
console.log(child1.name);
console.log(child1.getName());
console.log(child2.name);
console.log(child1.list);
console.log(child2.list);
原型式继承
原型式继承

5. 寄生式继承

const Parent1 = {
    name: '张三',
    list: [1,2,3],
    getName: function() {
        return this.name
    }
}
function clone(parent) {
    const clone = Object.create(parent)
    clone.getList = function() {
        return this.list
    }
    return clone
}
const child1 = clone(Parent1)
const child2 = clone(Parent1)
child1.name = 'child1'
console.log(child1.name);
console.log(child2.name);
console.log(child1.getName());
console.log(child2.getName());
console.log(child1.getList());

/*********************传统寄生式继承************************/
// 设置父类自有属性和方法
let parent2 = {
    name: 'zy',
    hobbies: ['tennis', 'music', 'photography'],
    getName: function () { console.log(this.name) }
}
// 这个方法用于创建一个新对象并且连接原型链
function object(obj) {
    function F() { }
    F.prototype = obj;
    return new F();
}
function createson(o, sex) {
    // 传入父类创建个新对象  
    let newson = object(o)
    // 这里增强对象,添加属性和方法
    newson.sex = sex
    newson.getsex = function () { console.log(this.sex) }
    // 返回对象
    return newson
}
let sub2 = createson(parent2, 'famle')
console.log(sub2)
sub2.getName()
sub2.getsex()
寄生式继承

6. 寄生组合式继承

 function Parent1() {
    this.name = 'Parent1',
    this.list = [1,2,3]
}
Parent1.prototype.getList = function() {
    return this.list
}
function Child1() {
    Parent1.call(this)
}
Child1.prototype = Object.create(Parent1.prototype);
Child1.prototype.construct = Child1
const child1 = new Child1()
const child2 = new Child1()
const child3 = new Child1()
child1.list.push(1)
child1.name = 'child1'
console.log(child1.name);
console.log(child2.name);
console.log(child1.getList());
console.log(child2.getList());
寄生组合式继承

相关文章

  • JavaScript 继承

    继承是JS中非常内容,原因就是JS没有地道的继承方式,我们只能通过各种方式来模拟面向对象中的继承。下面介绍几种常见...

  • js常见的继承方式

    1.原型链继承 基于原型链查找的特点,我们将父类的实例作为子类的原型,这种继承方式便是原型链继承。 Child.p...

  • js常见的继承方式

    js常见的继承方式 1. 原型链继承 2. 构造函数继承 3. 组合式继承 4. 原型式继承 5. 寄生式继承 6...

  • JavaScript常见的继承方式

    前言 JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一。那么在JS中常见的继承方式有几种呢? 方式一、原...

  • JS对象(2)- 常见的继承方式

    本文归纳js中常见的几种继承方式。通过如下的对象作为示例的父对象: 1. 原型链继承 子类的原型指向父类的实例来实...

  • 常见继承方式

    原型链+借用构造函数的组合继承 在子类的构造函数中通过parent.call(this)继承父类的属性,然后改变子...

  • js 的继承的几种方式

    js 继承有6种方式的代码。 js继承的6种方式[https://www.cnblogs.com/Grace-zy...

  • 探究JS常见的6种继承方式

    继承可以使得子类别具有父类的各种方法和属性 继承方法: 一、原型链继承 原型链继承是比较常见的继承方式之一其中涉及...

  • 继承

    研究学习了js内部的继承方式,以及多种方式的优点和缺点 目前项目中的 以前项目中的 js中继承有多种方式 原型继承...

  • JS继承

    JS中的继承 许多OO语言都支持两种继承方式:接口继承和实现继承; 因为JS中没有类和接口的概念 , 所以JS不支...

网友评论

      本文标题:js常见的继承方式

      本文链接:https://www.haomeiwen.com/subject/apevwltx.html