Question Bank

JavaScript Inherit x From Function b: Two Approaches Quiz

Difficulty: Medium

Make constructor `b` inherit `x` from constructor `a`: parent-constructor call via `Function.prototype.call`, ES6 `extends` + `super`, `Object.create` chaining, and an arrow-vs-constructor explainer.

Question Bank
/

JavaScript Inherit x From Function b: Two Approaches Quiz

JavaScript Inherit x From Function b: Two Approaches Quiz

Make constructor `b` inherit `x` from constructor `a`: parent-constructor call via `Function.prototype.call`, ES6 `extends` + `super`, `Object.create` chaining, and an arrow-vs-constructor explainer.

Question Bank
Medium
JavaScript
4 questions
quiz
closures
inheritance
interview-prep

705 views

4

Modify constructor b below so that calling new b(1, 2) produces an instance with BOTH y: 2 and x: 1. Use Function.prototype.call to invoke a with this bound to the new b instance.

Examples

Example 1:

Input:
const a = function (x) { this.x = x; };
const b = function (x, y) { this.y = y; };
const result = new b(1, 2);
Output before fix: b { y: 2 }
Output after fix:  b { y: 2, x: 1 }
Explanation: a.call(this, x) wires a's assignment onto b's instance.