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>

猜你喜欢

转载自www.cnblogs.com/themost/p/9365862.html