0
0
Javascriptprogramming~20 mins

Prototype chain in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prototype Chain Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
A"Hello, Alice"
Bundefined
C"Hello, undefined"
DTypeError: alice.greet is not a function
Attempts:
2 left
💡 Hint
Remember that methods defined on the prototype are accessible to instances.
Predict Output
intermediate
2: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);
A"red"
BTypeError
Cundefined
D"blue"
Attempts:
2 left
💡 Hint
Instance properties override prototype properties with the same name.
Predict Output
advanced
2: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);
A"generic" then "generic"
B"generic" then "bark"
C"bark" then "bark"
DTypeError
Attempts:
2 left
💡 Hint
Prototype properties are looked up dynamically at access time.
Predict Output
advanced
2: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());
ATypeError: Cannot read properties of undefined (reading 'toString')
Bundefined
Cnull
DReferenceError
Attempts:
2 left
💡 Hint
Accessing a property on undefined causes an error.
🧠 Conceptual
expert
3:00remaining
Which prototype chain lookup order is correct?
Given an object instance, in what order does JavaScript look up properties in the prototype chain?
AInstance properties → Object.prototype → Constructor.prototype → null
BConstructor.prototype → Instance properties → Object.prototype → null
CInstance properties → Constructor.prototype → Object.prototype → null
DObject.prototype → Constructor.prototype → Instance properties → null
Attempts:
2 left
💡 Hint
Think about where properties are first checked: own properties or prototype.