原型练习题

 

 

分析

后面的代码用child函数创建的对象的原型对象是 child1.prototype=o1{a:1,b:[1,2,1],c:{demo:5},show:上面的函数,__proto__:o3{}}

parent=o2{a:1,b:[1,2,1],c:{demo:5},show:上面的函数,__proto__:o3{}}

child2={a:12,change:上面的函数,__proto__:o1}

child1={a:11,change:上面的函数,__proto__:o1}

运行Parent()函数 console.log(this.a, this.b, this.c.demo);===>parent就是this也是o2对象

o2.a==>1 o2.b===>[1,2,1] o2.c.demo====>5

1 [1,2,1]  5

show函数是原型对象的方法 child1自己没有 访问原型的 就是调用者(函数内部的this)就是child()

====>console.log(this.a, this.b, this.c.demo);

this.a===>11 this.b===>[1,2,1] this.c.demo====>5       

11  [1,2,1]  5

show函数是原型对象的方法 child2自己没有 访问原型的 就是调用者(函数内部的this)就是child()

 ====>console.log(this.a, this.b, this.c.demo);

 this.a===>12 this.b===>[1,2,1] this.c.demo====>5  

 12 [1,2,1] 5

//this是child1

 this.b.push(this.a);

 b是原型对象的属性,但是它没有修改B,只是操作了b内部的数据是可以让原型对象的内部对象改变this.a取值会优先取原型链顶层的数据11==>原型对象的数组中添加11

       

this.a = this.b.length;

给child自己添加a属性 值为4 child1的原型对象的属性b的长度

this.c.demo = this.a++;

this.c。demo=先取值4 再把a的值加一

this.c属性是原型对像的属性  但是没有修改这个属性情况下 可以操作起内部的数据

因此原型对象的c属性中的demo属性改为4

this是child2

this.b.push(this.a);

 b是原型对象的属性,但是它没有修改B,只是操作了b内部的数据是可以让原型对象的内部对象改变

this.a取值会优先取原型链顶层的数据12==>原型对象的数组中添加12

       

 this.a = this.b.length;

给child自己添加a属性 值为5 child2的原型对象的属性b的长度

 this.c.demo = this.a++;

this.c.demo=先取值5 再把a的值加一

this.c属性是原型对像的属性  但是没有修改这个属性情况下 可以操作起内部的数据

因此原型对象的c属性中的demo属性改为5

运行Parent()函数 console.log(this.a, this.b, this.c.demo);===>parent就是this也是o2对象

o2.a==>1 o2.b===>[1,2,1] o2.c.demo====>5

1 [1,2,1]  5

show函数内部this是parent

 ====>console.log(this.a, this.b, this.c.demo);

this.a===>5 this.b====>[1,2,1,11,12] this.c.demo====>5

5 [1,2,1,11,12] 5

show函数内部this是parent

====>console.log(this.a, this.b, this.c.demo);

this.a===>6 this.b====>[1,2,1,11,12] this.c.demo====>5

6 [1,2,1,11,12] 5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值