JavaScript proto 原型链 问题

这是一个JavaScript 原型链的面试题
function Parent() {
            this.a = 1;
            this.b = [1, 2, this.a];
            this.c = { demo: 5 };
            this.show = function () {
                console.log(this.a , this.b , this.c.demo );
           }
}
function Child() {
    this.a = 2;
    this.change = function () {
        this.b.push(this.a);
        this.a = this.b.length;
        this.c.demo = this.a++;
    }
}
Child.prototype = new Parent(); 
var parent = new Parent();
var child1 = new Child();
var child2 = new Child();
child1.a = 11;
child2.a = 12;
parent.show();
child1.show();
child2.show();
child1.change();
child2.change();
parent.show();
child1.show();
child2.show();
设及的 知识点
  1. javascript proto 原型链
  2. javascript 计算方法
  3. this 指向问题
  4. 构造函数
话不多说,开始具体解析
Child.prototype = new Parent();
// 1. Child.prototype.constructor === Parent ; // true;
// 2. Child.prototype.__proto === Parent.prototype ; // true ;
// 3. Child.prototype = {
//  a:1,
//  b:[1,2,1],
//  c:{demo:5},
//  show:function
//  }
var parent = new Parent();
// 构造函数创建对象 parent 
// parent:{
//  a:1,
//  b:[1,2,1],
//  c:{demo:5},
//  show:function
//  }
var child1 = new Child();
// child1 : {  a: 2 }
// child1.__proto__ === Child.prototype; true
var child2 = new Child();
// child2 : {  a: 2 }
// child2.__proto__ === Child.prototype; true
// child2.__proto__ === child1.__proto__; true
parent.show();
/// parent:{
//  a:1,
//  b:[1,2,1],
//  c:{demo:5},
//  show:function
//  }

child1.show();
// 因为 child1 没有 this.b 也没有 this.c 所以从原型链上找到 child1.__proto__ ; 
// 结果 11, [1,2,1],5
child2.show();
// 因为 child2 没有 this.b 也没有 this.c 所以从原型链上找到 child2.__proto__ ;
// 结果 12, [1,2,1],5

child1.change();

//   this.b.push(this.a);   由于child1 本身没有this.b 所以从原型链上找  child1.__proto__.b  = [1,2,1,11]
//   this.a = this.b.length; //  child1.a  = 4
 //   this.c.demo = this.a++;  由于child1 本身没有this.c所以从原型链上找  child1.__proto__.c  = child1.a++ ; 则为 4  因为 child1.a++ ; child.,a = 5;

child2.change();
//   this.b.push(this.a);   由于child2 本身没有this.b 所以从原型链上找  child2.__proto__.b; 此时child2.__proto__ === child1.__proto__ ; // child2.__proto__.b= [1,2,1,11,12]
//   this.a = this.b.length; //  child2.a  = 5
 //   this.c.demo = this.a++;  由于child2 本身没有this.c所以从原型链上找  child2.__proto__.c  = child.a++ ; 则为 5  因为 child2.a++ ; child2.,a =6 
parent.show(); //1, [1,2,1], 5
child1.show(); //5, [1,2,1,11,12], 5  因为 child1.__proto__ === child2.__proto__  true ; child1.prototype 等于 Child.prototype  等于  child2.__proto__;
child2.show(); //6, [1,2,1,11,12], 5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JavaScript中,每个对象都有一个隐藏的 `__proto__` 属性,它指向该对象的原型。原型是一个普通的对象,它包含了共享属性和方法。当我们访问一个对象的属性或方法时,JavaScript引擎会首先在该对象本身查找,如果找不到,则会沿着原型链向上查找,直到找到该属性或方法,或者到达原型链的顶端(Object.prototype)。这个原型链的过程就是通过 `__proto__` 属性来实现的。 例如,当我们创建一个实例对象 `teacher` 时,如果 `teacher` 对象本身没有 `teach()` 方法,JavaScript引擎会通过对象的 `__proto__` 属性查找到 `Teacher.prototype` 的显式原型上,如果 `Teacher.prototype` 仍然没有该方法,它会继续沿着 `Teacher.prototype.__proto__` 找到 `Person.prototype`,直到找到 `teach()` 方法并执行。这样就形成了一个原型链。 同时,可以注意到 `Object.prototype.__proto__` 的值为 `null`,即 `Object.prototype` 没有原型。所以在原型链中,当找到顶层原型还没有属性时,就会返回 `undefined`。 需要注意的是, `__proto__` 是一个非标准的属性,实际开发中不应该直接使用它。它只是内部指向原型对象 `prototype` 的一个指示器,我们应该使用 `Object.getPrototypeOf()` 或 `Object.setPrototypeOf()` 来访问和设置对象的原型。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [JS:原型和原型链](https://blog.csdn.net/elevenhope/article/details/122882582)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [JavaScript原型链(重要)](https://blog.csdn.net/liyuchenii/article/details/125957625)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值