Challenge - 5 Problems
Object.create Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code using Object.create?
Consider the following JavaScript code. What will be logged to the console?
Javascript
const parent = { greeting: 'Hello' }; const child = Object.create(parent); child.greeting = 'Hi'; console.log(child.greeting);
Attempts:
2 left
💡 Hint
Remember that properties set directly on the child object override those from the prototype.
✗ Incorrect
The child object has its own 'greeting' property set to 'Hi', so it logs 'Hi'. The parent's 'greeting' is shadowed.
❓ Predict Output
intermediate2:00remaining
What does this code output when accessing inherited property?
Look at this code snippet. What will be the output of the console.log statement?
Javascript
const animal = { eats: true }; const rabbit = Object.create(animal); console.log(rabbit.eats);
Attempts:
2 left
💡 Hint
Object.create sets the prototype, so properties can be inherited.
✗ Incorrect
The 'rabbit' object inherits 'eats' from 'animal', so it logs true.
❓ Predict Output
advanced2:00remaining
What is the output of this code with property descriptors?
Analyze this code. What will be printed to the console?
Javascript
const proto = { x: 10 }; const obj = Object.create(proto, { y: { value: 20, writable: false, enumerable: true, configurable: true } }); obj.y = 30; console.log(obj.y);
Attempts:
2 left
💡 Hint
Writable: false means the property cannot be changed.
✗ Incorrect
The property 'y' is not writable, so the assignment to 30 fails silently and 'y' remains 20.
❓ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this code produce when run?
Javascript
const obj = Object.create(null); console.log(obj.toString());
Attempts:
2 left
💡 Hint
Object.create(null) creates an object with no prototype.
✗ Incorrect
Since obj has no prototype, it does not have the toString method, causing a TypeError.
🧠 Conceptual
expert3:00remaining
Which option correctly creates an object with a prototype and a new method?
You want to create an object 'car' that inherits from 'vehicle' and adds a method 'drive'. Which code does this correctly?
Attempts:
2 left
💡 Hint
Remember that the second argument of Object.create expects property descriptors.
✗ Incorrect
Option B correctly uses a property descriptor with a function value and sets enumerable true. Option B misses enumerable, A adds method after creation, D uses wrong descriptor syntax.