Challenge - 5 Problems
Prototype Chain Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of accessing a property through the prototype chain?
Consider the following code. What will be logged to the console?
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. Calling alice.greet() returns 'Hello, Alice'.
❓ Predict Output
intermediate2:00remaining
What happens when a property is shadowed on the instance?
Look at this code. What will be the output of console.log(car.color)?
Javascript
function Vehicle() {} Vehicle.prototype.color = 'red'; const car = new Vehicle(); car.color = 'blue'; console.log(car.color);
Attempts:
2 left
💡 Hint
Instance properties override prototype properties with the same name.
✗ Incorrect
The instance car has its own color property set to 'blue', which shadows the prototype's 'red'. So car.color is 'blue'.
❓ Predict Output
advanced2:00remaining
What is the output when modifying the prototype after instance creation?
What will this code print to the console?
Javascript
function Animal() {} Animal.prototype.sound = 'generic'; const dog = new Animal(); console.log(dog.sound); Animal.prototype.sound = 'bark'; console.log(dog.sound);
Attempts:
2 left
💡 Hint
Prototype properties are looked up dynamically at access time.
✗ Incorrect
dog.sound first reads 'generic'. After changing Animal.prototype.sound to 'bark', dog.sound reflects the new value.
❓ Predict Output
advanced2:00remaining
What error occurs when accessing a missing property in the prototype chain?
What will happen when this code runs?
Javascript
const obj = {}; console.log(obj.someProperty.toString());
Attempts:
2 left
💡 Hint
Accessing a property on undefined causes an error.
✗ Incorrect
obj.someProperty is undefined. Calling toString() on undefined causes a TypeError.
🧠 Conceptual
expert3:00remaining
Which prototype chain lookup order is correct?
Given an object instance, in what order does JavaScript look up properties in the prototype chain?
Attempts:
2 left
💡 Hint
Think about where properties are first checked: own properties or prototype.
✗ Incorrect
JavaScript first checks own properties on the instance, then the constructor's prototype, then Object.prototype, then null.