js——继承

js——继承

继承:两个类之间的 is a kind of 关系
实现继承:子类拥有父类的全部属性和方法,还可以拥有自己独有的方法和属性
通过原型链实现继承:子类的原型指向父类的实例 子类.prototype=new 父类() 子类可以重写/复写父类的方法

在这里插入图片描述

在这里插入代码片
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>js继承:子类的原型指向父类的实例</title>
</head>

<body>
    <script>
        // 
        function People(name, age) {
            this.name = name
            this.age = age
        }
        People.prototype.sayHello = function () {
            console.log(this.name + ":hello");
        }

        // 这里,子类的参数要包含父类的参数
        function Student(name, age, sex) {
            this.name = name
            this.age = age
            this.sex = sex
        }

        // 创建出子类后书写继承语句 子类.prototype=new 父类()
        Student.prototype = new People()

        Student.prototype.jude = function () {
            console.log(this.sex);
        }

        // 测试
        var stu = new Student('sam', 12, 'woman')
        stu.sayHello()
        stu.jude()
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值