构造继承

作者: 杰克_王_ | 来源:发表于2019-10-20 14:12 被阅读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>
            /*
            call 是以对象列表的方式进行传参
            apply 使用数组的方式进行传参
            */
            /*
            不能继承原型方法
            */
    
            function Animal(category) {
                this.category = category || "Animal";
                this.run = function () {
                    console.log("Animal run");
                }
            }
    
            Animal.prototype.walk = function () {
                console.log("Animal walk");
            }
    
            function Dog(category, name) {
                Animal.call(this, category);
                // Animal.apply(this, [category]);
                this.name = name || 'dog';
                this.type = 'dog';
            }
    
            var dog = new Dog("dog", "小白");
            console.log(dog);
        </script>
    </body>
    
    </html>
    

    相关文章

      网友评论

        本文标题:构造继承

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