请问这俩添加的原型,是添加到了哪里,是添加到了构造函数People身上吗

请问这俩添加的原型,是添加到了哪里,是添加到了构造函数People身上吗

https://img1.sycdn.imooc.com//climg/6320756209088de813330874.jpg

<!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>Document</title>
</head>

<body>
    <script>
        // 父类
        function People(name, age, sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        };
        People.prototype.sayHello = function () {
            console.log('我是' + this.name + ',我今年' + this.age);
        };
        People.prototype.sleep = function () {
            console.log(this.name + '开始睡觉');
        };

        // 子类
        function Student(name, age, sex, school, id) {
            this.name = name;
            this.age = age;
            this.sex = sex;
            this.school = school;
            this.id = id;
        };
        // 实现继承
        Student.prototype = new People();
        Student.prototype.study = function () {
            console.log('我正在学习');
        };
        Student.prototype.exam = function () {
            console.log('我正在考试');
        };

        var hanmeimei = new Student('韩梅梅', 20, '女');
        hanmeimei.sayHello();
        hanmeimei.sleep();
    </script>
</body>

</html>


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,同学理解是对的,祝学习愉快~

  • 张小阳_ 提问者 #1

    老师,它俩不应该是添加到了构造函数Student身上吗?

    2022-12-18 19:59:51
  • imooc_慕慕 回复 提问者 张小阳_ #2

    同学你好,准确来说是在people的实例上,因为代码中有这样一句代码:

    Student.prototype = new People();

    Student的原型指向了people的实例,说在student原型上的方法也可以。

    我们可以打印一下Student 的实例hanmeimei

    https://img1.sycdn.imooc.com//climg/639fc256099e8fc707780272.jpg

    祝学习愉快~


    2022-12-19 09:46:18
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师