组合继承

作者: 杰克_王_ | 来源:发表于2019-10-20 14:13 被阅读0次
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>组合继承</title>
</head>

<body>
    <script>
        function Animal(category) {
            this.category = category || "Animal";
        }

        Animal.prototype.run = function () {
            console.log("Animal run");
        }

        function Dog(category, name) {
            Animal.call(this, category); // 继承示例属性
            // Animal.apply(this, [category]);
            this.name = name || 'dog';
            this.type = 'dog';
        }

        Dog.prototype = Object.assign({}, Animal.prototype); // 继承原型方法,修改不会同步继承
        // Dog.prototype = Object.create(Animal.prototype); // 继承原型方法,修改会同步继承
        Dog.prototype.constructor = Dog; // 复原原型指向

        var dog = new Dog("dog", "小白");

        Animal.prototype.walk = function () {
            console.log("Animal walk");
        }

        console.log(dog);

        for (var name in Animal.prototype) {
            console.log(name);
            console.log(Animal.prototype[name]);
        }
    </script>
</body>

</html>

相关文章

  • 继承

    原型继承 借用构造函数 组合继承 原型式继承 寄生式继承 寄生组合继承 优点: 因为组合继承最大的问题是无论什么...

  • 二、js继承的几种方式及优缺点

    1、继承:原型链、借用构造函数、组合继承、原型式继承、寄生式继承、寄生组合继承

  • 组合继承,寄生组合继承,class继承

    组合继承 说明:核心是在子类的构造函数中通过 Parent.call(this) 继承父类的属性,然后改变子类的原...

  • JS继承方式总结 (转)

    借用构造函数继承 原型链式继承(借用原型链实现继承) 组合式继承 组合式继承优化1 组合式继承优化2 ES6中继承...

  • js继承方式

    类式继承 构造函数继承 组合继承 类式继承 + 构造函数继承 原型式继承 寄生式继承 寄生组合式继承 寄生式继承 ...

  • js之继承

    文章主讲 JS 继承,包括原型链继承、构造函数继承、组合继承、寄生组合继承、原型式继承、 ES6 继承,以及 多继...

  • javaScript 实现继承方式

    JavaScript实现继承共6种方式:原型链继承、借用构造函数继承、组合继承、原型式继承、寄生式继承、寄生组合式继承。

  • ES5和ES6 实现继承方式

    在ES5 中:通过原型链实现继承的,常见的继承方式是组合继承和寄生组合继承;在ES6中:通过Class来继承 组合...

  • (九)

    寄生组合式继承前面说过,组合继承是JavaScript最常用的继承模式;不过,它也有自己的不足。组合继承最大的问题...

  • PHP学习2

    六.继承与多态 1. 类的组合和继承(继承===“是、像”、“父与子”,组合===“需要”、“整体与局部”) 组合...

网友评论

    本文标题:组合继承

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