Challenge - 5 Problems
Object Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of Object.create with prototype properties
What is the output of the following code?
Javascript
const proto = { greeting: 'hello' }; const obj = Object.create(proto); obj.greeting = 'hi'; console.log(obj.greeting); console.log(proto.greeting);
Attempts:
2 left
π‘ Hint
Remember that Object.create sets the prototype, and own properties override prototype properties.
β Incorrect
The object obj has its own property greeting set to 'hi', which overrides the prototype's greeting 'hello'. So obj.greeting logs 'hi'. The prototype object proto remains unchanged, so proto.greeting logs 'hello'.
β Predict Output
intermediate2:00remaining
Output of Object.create with null prototype
What will be the output of this code?
Javascript
const obj = Object.create(null); console.log(typeof obj.toString);
Attempts:
2 left
π‘ Hint
Object.create(null) creates an object with no prototype, so it has no inherited methods.
β Incorrect
Since obj has no prototype, it does not inherit toString method. So obj.toString is undefined and typeof undefined is 'undefined'.
π§ Debug
advanced2:00remaining
Why does this object creation code throw an error?
The following code throws an error. Which option explains the cause?
Javascript
const obj = Object.create();
Attempts:
2 left
π‘ Hint
Check the required parameters for Object.create.
β Incorrect
Object.create must be called with a prototype object as the first argument. Omitting it causes a TypeError because undefined is not an object.
β Predict Output
advanced2:00remaining
Output of Object.create with property descriptors
What is the output of this code?
Javascript
const obj = Object.create({}, { name: { value: 'Alice', writable: false, enumerable: true } }); obj.name = 'Bob'; console.log(obj.name);
Attempts:
2 left
π‘ Hint
Check if the property is writable or not.
β Incorrect
The property 'name' is created as non-writable, so attempts to change it are ignored. The value remains 'Alice'.
π§ Conceptual
expert3:00remaining
Which option correctly creates an object with a prototype and own properties?
You want to create an object that inherits from a prototype object and also has its own property 'age' set to 30. Which code does this correctly?
Attempts:
2 left
π‘ Hint
Remember Object.create's second argument requires property descriptors.
β Incorrect
Option B correctly uses Object.create with a prototype and defines 'age' as an own property with proper descriptors. Option B adds 'age' after creation but does not use property descriptors. Option B incorrectly passes a number instead of a descriptor object. Option B creates a new object copying properties but does not set the prototype.