0
0
Javascriptprogramming~20 mins

Object creation in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Object Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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);
A"hi" and then "hello"
B"hello" and then "hi"
Cundefined and then "hello"
D"hi" and then "hi"
Attempts:
2 left
πŸ’‘ Hint
Remember that Object.create sets the prototype, and own properties override prototype properties.
❓ Predict Output
intermediate
2: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);
ATypeError
B"function"
C"object"
D"undefined"
Attempts:
2 left
πŸ’‘ Hint
Object.create(null) creates an object with no prototype, so it has no inherited methods.
πŸ”§ Debug
advanced
2: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();
AObject.create only accepts arrays as arguments.
BObject.create cannot be called without the new keyword.
CObject.create requires a prototype object argument; calling it without arguments causes a TypeError.
DObject.create requires a second argument for property descriptors.
Attempts:
2 left
πŸ’‘ Hint
Check the required parameters for Object.create.
❓ Predict Output
advanced
2: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);
A"Alice"
B"Bob"
Cundefined
DTypeError
Attempts:
2 left
πŸ’‘ Hint
Check if the property is writable or not.
🧠 Conceptual
expert
3: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?
A
const proto = { greet() { return 'hi'; } };
const obj = Object.create(proto, { age: 30 });
B
const proto = { greet() { return 'hi'; } };
const obj = Object.create(proto, { age: { value: 30, writable: true, enumerable: true } });
C
const proto = { greet() { return 'hi'; } };
const obj = Object.create(proto);
obj.age = 30;
D
const proto = { greet() { return 'hi'; } };
const obj = { ...proto, age: 30 };
Attempts:
2 left
πŸ’‘ Hint
Remember Object.create's second argument requires property descriptors.