MST

星途 面试题库

面试题:JavaScript 函数 this 绑定与原型链综合考察

请补全以下代码,使得 `console.log(instance.getValue())` 输出 `3`。要求遵循 `JavaScript` 函数 `this` 绑定规则,同时合理利用原型链相关知识。 ```javascript function Parent() { this.value = 1; } function Child() { // 在此处补全代码 } // 在此处补全代码 const instance = new Child(); ```
31.8万 热度难度
编程语言JavaScript

知识考点

AI 面试

面试题答案

一键面试
function Parent() {
    this.value = 1;
}
function Child() {
    Parent.call(this);
    this.value = 3;
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
const instance = new Child();
console.log(instance.getValue());