Challenge - 5 Problems
Prototype Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of prototype property access
What is the output of the following code?
Javascript
function Person(name) { this.name = name; } Person.prototype.greet = function() { return `Hello, ${this.name}`; }; const alice = new Person('Alice'); console.log(alice.greet());
Attempts:
2 left
💡 Hint
Remember that methods defined on the prototype are accessible to instances.
✗ Incorrect
The greet method is defined on Person.prototype, so alice inherits it and can call it. The name property is set to 'Alice', so the output is "Hello, Alice".
❓ Predict Output
intermediate2:00remaining
Effect of changing prototype after instance creation
What will be the output of this code?
Javascript
function Animal() {} Animal.prototype.sound = function() { return 'generic sound'; }; const dog = new Animal(); Animal.prototype = { sound: function() { return 'bark'; } }; console.log(dog.sound());
Attempts:
2 left
💡 Hint
Think about when the prototype is linked to the instance.
✗ Incorrect
The dog instance keeps the original prototype linked at creation. Changing Animal.prototype later does not affect existing instances. So dog.sound() calls the original method returning "generic sound".
🔧 Debug
advanced2:00remaining
Why does this prototype chain fail?
The following code tries to create a prototype chain where Student inherits from Person. Why does calling student.introduce() cause an error?
Javascript
function Person(name) { this.name = name; } Person.prototype.introduce = function() { return `Hi, I'm ${this.name}`; }; function Student(name, subject) { Person.call(this, name); this.subject = subject; } Student.prototype = Person; Student.prototype.constructor = Student; const student = new Student('Bob', 'Math'); console.log(student.introduce());
Attempts:
2 left
💡 Hint
Check how the prototype is assigned for Student.
✗ Incorrect
Assigning Student.prototype = Person assigns the function Person itself, not its prototype object. So student.__proto__ is a function, which does not have introduce method. The correct way is Student.prototype = Object.create(Person.prototype).
🧠 Conceptual
advanced2:00remaining
Prototype chain property lookup order
Given the following objects, which property value will be logged?
Javascript
const grandparent = { trait: 'wisdom' }; const parent = Object.create(grandparent); parent.trait = 'strength'; const child = Object.create(parent); console.log(child.trait);
Attempts:
2 left
💡 Hint
Remember how property lookup works in prototype chains.
✗ Incorrect
child does not have trait, so it looks up to parent which has trait = 'strength'. It returns that value without checking grandparent.
❓ Predict Output
expert2:00remaining
Output of modifying prototype after instance creation with method override
What is the output of this code?
Javascript
function Vehicle() {} Vehicle.prototype.move = function() { return 'moving'; }; const car = new Vehicle(); Vehicle.prototype.move = function() { return 'driving'; }; console.log(car.move());
Attempts:
2 left
💡 Hint
Think about how methods on the prototype are looked up at call time.
✗ Incorrect
car.move looks up the move method on Vehicle.prototype at call time. Since the method was replaced after car was created, car.move calls the new method returning "driving".