js中的hasOwnProperty

<!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");
            }
        };

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



        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");
            }
        };
        //hasOwnProperty,如果属性是在实例中定义的返回true,如果是在圆型定义返回false


        var barnaby = new Dog("Barnaby", "Basset Hound", 55);
        console.log(barnaby.hasOwnProperty("sitting"));
        barnaby.sit();
        console.log(barnaby.hasOwnProperty("sitting"));

        console.log(fido.hasOwnProperty("sitting"));
        fido.sit();
        console.log(fido.hasOwnProperty("sitting"));
        

    </script>
</head>
<body>

</body>
</html>
原文地址:https://www.cnblogs.com/themost/p/9363995.html