麻烦老师解答,标识的地方代码的含义,还有属性的继承

麻烦老师解答,标识的地方代码的含义,还有属性的继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function People(name, age, sex) {
   this.name = name;
   this.age = age;
   this.sex = sex;
   this.arr = [1, 2, 3];
}
 
People.prototype.sayHello = function () {
   console.log("你好,我是" this.name);
};
 
function Student(name, age, sex, school, stuNum) {
   People.call(this, name, age, sex);
   this.school = school;
   this.stuNum = stuNum;
}
 
function Bread(color, name) {
   this.color = color;
   this.name = name;
}
 
//原型链继承,继承于同一个people
var xh = new Student("小明", 20, "女""清华大学", 1002);
xh.__proto__ = new People(); //-->这里
console.log(xh.color, xh.name);
xh.sayHello();
xh.__proto__ = new Bread("红色""面包"); //-->这里
console.log(xh.color);
console.log(xh.name);


正在回答 回答被采纳积分+1

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

1回答
好帮手慕星星 2022-12-10 16:14:48

同学你好,解答如下:

1、可以先看构造继承的结果

https://img1.sycdn.imooc.com//climg/63943de3095afb0107810601.jpg

实例对象xh的原型指向Student构造函数,也就是xh.__proto__的结果是Student构造函数

https://img1.sycdn.imooc.com//climg/63943df609e4233109160281.jpg

2、接下来改变实例原型指向为People实例

https://img1.sycdn.imooc.com//climg/63943e7d09c029f407250121.jpg

可以看到xh对象原型不再是Student构造函数,为People实例

https://img1.sycdn.imooc.com//climg/63943e9b0908581709800306.jpg

现在xh对象中没有color属性,输出值为undefined。name会先找实例上的,结果为‘小明’。

3、接下来改变实例原型指向为Bread实例

https://img1.sycdn.imooc.com//climg/63943f4109f4d85b06470220.jpg

原型指向会被覆盖为Bread实例,没有People实例了

https://img1.sycdn.imooc.com//climg/63943f700953a10b08890284.jpg

输出color值为‘红色’,输出name值实例上的值“小明”(如果实例上没有name属性才会输出原型上的值‘面包’)。

祝学习愉快!

  • 提问者 DanielDu87 #1
    var xh = new Student("小红", 20, "女", "清华大学", 1002);
    xh.__proto__ = new People("小明", 18, "男");

    改变实例原型指向为People带参数实例,但是输出属性时还是先查找实例属性,所以属性不会变,但是这个实例可以通过原型链查找使用people自带的sayHello方法了,其中的this指向Student实例,因为是实例调用,所以name就是小红

    老师理解对吗

    2022-12-10 16:40:30
  • 提问者 DanielDu87 #2
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    function People(name, age, sex) {
       this.name = name;
       this.age = age;
       this.sex = sex;
       this.arr = [1, 2, 3];
    }
     
    // People.prototype.arr = [1, 2, 3];
     
    People.prototype.sayHello = function () {
       console.log("你好,我是" this.name);
    };
     
    function Student(name, age, sex, school, stuNum) {
       People.call(this, name, age, sex);
       this.school = school;
       this.stuNum = stuNum;
    }
     
    function Bread(color, name) {
       this.color = color;
       this.name = name;
    }
     
    var xh = new Student("小红", 20, "女""清华大学", 1002);
    var xm = new Student("小明", 18, "男""清华大学", 1001);
    xh.__proto__ = xm.__proto__ = new People();
    xh.sayHello();
    xm.sayHello();
    console.log(xh.arr);
    console.log(xm.arr);
    xh.arr.push(4);
    console.log(xh.arr);
    console.log(xm.arr);

    这样写为什么两个实例的arr不会相互影响

    2022-12-10 17:55:34
  • 好帮手慕星星 回复 提问者 DanielDu87 #3

    1、是的 ,理解没问题。

    2、实例属性可以理解为私有的,每个实例对象互不影响。

    2022-12-10 18:33:22
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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