首页 > 试题广场 >

执行以下程序,输出结果为() function Father

[单选题]

执行以下程序,输出结果为()


function Father(age){
    this.age = age
}
function Son(age){
    Father.call(this);
}
Son.prototype = Father.prototype;
Father.prototype.getAge = function(){console.log(40);}
Son.prototype.getAge = function(){console.log(18);}
var father = new Father(40);
var son = new Son(18);
son.getAge();
father.getAge();


  • 18 18

  • 40 40
  • 18 40
  • 18 undefined
function Father(age) { //此时Father.prototype = { constructor: function Father(){}}
    this.age = age
}
function Son(age) { //此时Son.prototype = { constructor: function Son(){}}
    Father.call(this);
}

Son.prototype = Father.prototype;
//此时Son.prototype = { constructor: function Father(){}}   
//此时Father.prototype = { constructor: function Father(){}}
//现在,Son和Father的prototype指向同一个对象,也就是指向堆里的同一个地址


Father.prototype.getAge = function () { console.log(40) },//堆里加一个getAge属性
//此时:
// Father.prototype = {
//     constructor: function Father(){}
//     getAge: function(){console.log(40);}
// }
//特别注意,因为Son和Father的prototype指向同一个对象(堆里同一个地址),所以
// Son.prototype = {
//     constructor: function Father(){}
//     getAge: function(){console.log(40);}
// }

Son.prototype.getAge = function () { console.log(18) };
//此时:
// Son.prototype = {
//     constructor: function Father(){}
//     getAge: function(){console.log(18);}
// }
//因为Son和Father的prototype指向同一个对象(堆里同一个地址),所以
// Father.prototype = {
//     constructor: function Father(){}
//     getAge: function(){console.log(18);}
// }


var father = new Father(40); // father = { age: 40 } 
var son = new Son(18); //如果构造函数Son里没有Father.call(this);  那么此时 son = {}
//但是,构造函数Son里有Father.call(this);  然后 son = { age: undefined } (暂时不太明白这是为什么)

//以上结束后father = { age: 40 } 
//以上结束后son = { age: undefined } 


father.getAge(); //father里只有一个age,没有getAge方法,去原型Father.prototype里找, 执行函数console.log(18)
son.getAge();//son里只有一个age,没有getAge方法,去原型Son.prototype里找, 执行函数console.log(18)

//本题跟传入的age没有关系,因为getAge方法里没有任何对age的处理,只是执行一个console.log()

如果把题目改成如下形式,就必须知道Father.call(this);的作用才能答对了







编辑于 2022-01-28 16:05:08 回复(11)
father和son的prototype是同一个,son修改为18覆盖了father的修改,所以prototype里面的getage输出的是18,所以father和son都是输出18
发表于 2021-12-12 01:25:20 回复(4)
一点个人想法 众所周知一个函数被定义的时候,同时会有一个对应的prototype出现。 这里把son和father的prototype都指向了father的prototype。 father往prototype添加了getage函数,son随即把getage函数修改了值。 当son和father两个对象调用这个函数,在各自函数中找不到getage就会往他们各自的prototype中寻找。 那么两者的prototype又指向同一个。 所以,两者都是18。 不知道这样理解对不。
发表于 2022-01-01 17:30:44 回复(1)
这道题也是醉了,有点为了出题而出题的意思,绕来绕去还是回到了起点。。。
首先代码使用了组合继承,
然后:
Father.prototype.getAge = function(){console.log(40);}
Son.prototype.getAge = function(){console.log(18);}
这两个表达式中Son.prototype.getAge 会将 Father 构造函数的 getAge 原型方法替换掉
再然后这里的Son.prototype = Father.prototype;实际并不会生效,因为当执行到 var son = new Son(18) 表达式时,Son.prototype 会自动被赋值为 Father 构造函数的实例实现继承;
因此最后
son.getAge();
father.getAge();
调用的实际上都是 
function(){console.log(18);}
这个方法


发表于 2022-02-13 13:00:52 回复(0)
不会真有人这样写代码吧🥺
发表于 2023-04-10 20:39:36 回复(0)
function Father(age){ this.age = age } function Son(age){ Father.call(this); } 这里Son的原型对象和Father原型对象共享,都指向Father的原型对象 Son.prototype = Father.prototype; Father原型对象上新加getAge方法 Father.prototype.getAge = function(){console.log(40);} Son原型对象和Father原型对象共享,所以会覆盖Father原型对象上的getAge方法 Son.prototype.getAge = function(){console.log(18);} 这里father:{ age:40 } var father = new Father(40); 这里son:{age:undefined},空对象,因为call里面没有传age,无效,如果这样Father.call(this,age),这里son就会为:{age:18}, var son = new Son(18); 最后都调用getAge方法,调用的是father原型对象上的getAge方法,为18 son.getAge(); father.getAge();
发表于 2022-09-04 08:24:24 回复(0)
Son.prototype = Father.prototype; 
son的原型对象和father的原型对象一致
发表于 2022-08-13 16:43:12 回复(0)
为什么Son和Father的prototype是同一个啊
发表于 2022-05-07 11:32:49 回复(1)
是不是son.getAge()把Father的age改成了18,所以father.getAge()s q 18
发表于 2022-02-13 17:57:01 回复(0)