0
0
Javascriptprogramming~20 mins

Why prototypes are needed in Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prototype Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this prototype chain example?
Consider the following JavaScript code using prototypes. What will be printed to the console?
Javascript
function Animal() {
  this.alive = true;
}
Animal.prototype.breathe = function() {
  return 'Breathing';
};

function Dog(name) {
  this.name = name;
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function() {
  return 'Woof!';
};

const myDog = new Dog('Buddy');
console.log(myDog.bark());
console.log(myDog.breathe());
console.log(myDog.alive);
A
"Woof!"
"Breathing"
true
B
"Woof!"
undefined
true
CTypeError: myDog.bark is not a function
D
"Woof!"
"Breathing"
undefined
Attempts:
2 left
💡 Hint
Remember that Dog inherits from Animal through the prototype chain.
Predict Output
intermediate
2:00remaining
What error does this code raise?
What error will this code produce when run?
Javascript
const obj = {};
console.log(obj.toString());
obj.toString = 5;
console.log(obj.toString());
A5
Bundefined
CTypeError: obj.toString is not a function
DNo error, prints '[object Object]' then '5'
Attempts:
2 left
💡 Hint
What happens when you assign a non-function value to a method and then call it?
🧠 Conceptual
advanced
1:30remaining
Why are prototypes important in JavaScript?
Which of the following best explains why prototypes are needed in JavaScript?
ATo enforce strict typing of variables at runtime.
BTo allow objects to inherit properties and methods from other objects, enabling code reuse and efficient memory use.
CTo create private variables inside functions that cannot be accessed outside.
DTo automatically convert all objects to strings when printed.
Attempts:
2 left
💡 Hint
Think about how JavaScript shares methods between many objects.
Predict Output
advanced
2:00remaining
What is the output of this prototype modification?
What will this code print 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());

Person.prototype.greet = function() {
  return `Hi, ${this.name}`;
};

console.log(alice.greet());
A
"Hello, Alice"
"Hi, Alice"
B
"Hello, Alice"
"Hello, Alice"
CTypeError: alice.greet is not a function
D
"Hi, Alice"
"Hi, Alice"
Attempts:
2 left
💡 Hint
Remember that changing the prototype method affects all instances.
Predict Output
expert
2:30remaining
What is the output and why?
Analyze this code and select the correct output:
Javascript
const parent = { value: 42 };
const child = Object.create(parent);
child.value = 100;

console.log(child.value);
delete child.value;
console.log(child.value);
delete child.value;
console.log(child.value);
A
100
42
42
B
100
undefined
undefined
C
42
42
42
D
100
100
100
Attempts:
2 left
💡 Hint
Deleting a property only removes it from the object itself, not from its prototype.