instanceof

3 篇文章 0 订阅

instanceof

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        class People {
            constructor(name, age, sex) {
                this.name = name;
                this.age = age;
                this.sex = sex;
            }
            sayHello() {
                console.log("大家好,我是" + this.name + ",我今年" + this.age + "岁, 我是" + this.sex + "的")
            }
        }

        class Child extends People {
            constructor(name, age, sex, height, weight) {
                // super必须写在第一条
                super(name, age, sex);
                this.height = height;
                this.weight = weight;
            }
            jump() {
                console.log("u jump i jump")
            }
        }



        var c1 = new Child("小明", 13, "男", 160, 200);
        var p = new People("小红", 13, '女');


		//Child继承了people  所以c1作为child的实例  自然也包含在people中  子类包含在父类中
		//instanceof用于检测是不是类的实例
        console.log(c1 instanceof Child); // true 
        // 说明 c1 是 Child的实例

        console.log(c1 instanceof People); // true
        // 说明 c1 也是 People的实例
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值