0
0
Javascriptprogramming~20 mins

Prototype inheritance in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prototype Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
ATypeError: alice.greet is not a function
Bundefined
C"Hello, Alice"
D"Hello, undefined"
Attempts:
2 left
💡 Hint
Remember that methods defined on the prototype are accessible to instances.
Predict Output
intermediate
2: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());
A"bark"
B"generic sound"
CTypeError: dog.sound is not a function
Dundefined
Attempts:
2 left
💡 Hint
Think about when the prototype is linked to the instance.
🔧 Debug
advanced
2: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());
Aundefined
B"Hi, I'm Bob"
CReferenceError: introduce is not defined
DTypeError: student.introduce is not a function
Attempts:
2 left
💡 Hint
Check how the prototype is assigned for Student.
🧠 Conceptual
advanced
2: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);
A"strength"
B"wisdom"
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Remember how property lookup works in prototype chains.
Predict Output
expert
2: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());
A"driving"
Bundefined
CTypeError: car.move is not a function
D"moving"
Attempts:
2 left
💡 Hint
Think about how methods on the prototype are looked up at call time.