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>
  • 相关阅读:
    十大Intellij IDEA快捷键
    多媒体播放API 生命周期束&简介
    Bitmap
    Activity
    Android中的Handler总结
    Bitmap2
    smartimageview和多线程
    Service
    微软面试题 博弈论 经典案例 (参考答案)
    ANR和消息机制
  • 原文地址:https://www.cnblogs.com/themost/p/9365862.html
Copyright © 2011-2022 走看看