<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dog</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");
            }
        };

        var fido = new Dog("fido", "mixed", 38);
        fido.bark();

        //给原型添加方法后所有的继承者都拥有该方法,属性也是如此
        var barnaby = new Dog("Barnaby", "Basset Hound", 55);
        Dog.prototype.sit = function(){
            console.log(this.name + " is now sitting");
        };

        barnaby.sit();


    </script>
</head>
<body>

</body>
</html>

 

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2021-10-13
  • 2022-12-23
  • 2021-10-18
  • 2022-12-23
  • 2021-12-16
猜你喜欢
  • 2021-11-25
  • 2021-11-23
  • 2021-06-07
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案