0
0
Javascriptprogramming~20 mins

Object.create usage in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Object.create 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 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);
A"Hi"
B"Hello"
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Remember that properties set directly on the child object override those from the prototype.
Predict Output
intermediate
2: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);
Atrue
Bfalse
Cundefined
DReferenceError
Attempts:
2 left
💡 Hint
Object.create sets the prototype, so properties can be inherited.
Predict Output
advanced
2: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);
Aundefined
B30
C20
DTypeError
Attempts:
2 left
💡 Hint
Writable: false means the property cannot be changed.
Predict Output
advanced
2: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());
AReferenceError: toString is not defined
Bundefined
C[object Object]
DTypeError: obj.toString is not a function
Attempts:
2 left
💡 Hint
Object.create(null) creates an object with no prototype.
🧠 Conceptual
expert
3: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?
A
const vehicle = { wheels: 4 };
const car = Object.create(vehicle);
car.drive = function() { return 'Driving'; };
B
const vehicle = { wheels: 4 };
const car = Object.create(vehicle, { drive: { value: function() { return 'Driving'; }, enumerable: true } });
C
const vehicle = { wheels: 4 };
const car = Object.create(vehicle, { drive: { value: () => 'Driving' } });
D
const vehicle = { wheels: 4 };
const car = Object.create(vehicle, { drive: () => 'Driving' });
Attempts:
2 left
💡 Hint
Remember that the second argument of Object.create expects property descriptors.