zoukankan      html  css  js  c++  java
  • js中的原型继承

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Dogadd2</title>
        <script>
    
            function Dog(name, breed, weight){
                this.name = name;
                this.breed = breed;
                this.weight = weight;
            }
            Dog.prototype.species = "Canine";
            Dog.prototype.bark = function(){
                if (this.weight > 25){
                    console.log(this.name + " Woof");
                } else {
                    console.log(this.name + " Yip!")
                }
            };
            Dog.prototype.sitting = false;
            Dog.prototype.sit = function(){
                if (this.sitting){
                    console.log(this.name + " is already sitting");
                } else {
                    this.sitting = true;
                    console.log(this.name + " is start sitting");
                }
            };
    
    
            var fido = new Dog("fido", "mixed", 38);
            var barnaby = new Dog("Barnaby", "Basset", 55);
    
            //构造继承小狗原型的表演犬原型
            function ShowDog(name, breed, weight, handler){
                //调用dog.call传入参数
                Dog.call(this, name, breed, weight);
                this.handler = handler;
            }
    
            ShowDog.prototype = new Dog();
            //constructor显式的设置构造函数
            ShowDog.prototype.constructor = ShowDog;
    
            ShowDog.prototype.league = "Webville";
    
            ShowDog.prototype.stack = function(){
                console.log("Stack");
            };
    
            ShowDog.prototype.bait = function(){
                console.log("Bait");
            };
    
            ShowDog.prototype.gait = function(){
                console.log(kind + " ing");
            };
    
            ShowDog.prototype.groom = function(){
                console.log("Groom");
            };
    
            var scotty = new ShowDog("Scotty", "Scottish Terrier", 35, "Cookie");
            scotty.bark();
            scotty.bait();
            
    
        </script>
    </head>
    <body>
    
    </body>
    </html>
  • 相关阅读:
    如何增加学习感悟
    古代到清朝历史及文化
    至少的问题
    两次相遇同一地点
    行测都是选择题总结经验
    DES加密实现的思想及代码
    AES算法
    线性代数中行列与矩阵的联系和区别
    学习phalcon框架按照官网手册搭建第一个项目注册功能
    phpadmin增加使得项目能连接数据库
  • 原文地址:https://www.cnblogs.com/themost/p/9365862.html
Copyright © 2011-2022 走看看