0
0
Javascriptprogramming~10 mins

Object.create usage in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new object with the specified prototype.

Javascript
const proto = { greet() { return 'Hello'; } };
const obj = Object.[1](proto);
console.log(obj.greet());
Drag options to blanks, or click blank then click option'
Aassign
BdefineProperty
Ccreate
DsetPrototypeOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using Object.assign instead of Object.create.
Trying to use Object.setPrototypeOf to create an object.
Using Object.defineProperty which is for properties, not creating objects.
2fill in blank
medium

Complete the code to add a property to the new object created with Object.create.

Javascript
const proto = { type: 'animal' };
const obj = Object.create(proto);
obj.[1] = 'dog';
console.log(obj.type, obj.name);
Drag options to blanks, or click blank then click option'
Atype
Bname
Cspecies
Dbreed
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to overwrite the prototype property 'type'.
Adding a property that already exists on the prototype.
Confusing prototype properties with own properties.
3fill in blank
hard

Fix the error in the code to properly create an object with a prototype and a property descriptor.

Javascript
const proto = { speak() { return 'Hi'; } };
const obj = Object.create(proto, {
  age: { [1]: 5, writable: true }
});
console.log(obj.age, obj.speak());
Drag options to blanks, or click blank then click option'
Avalue
Bconfigurable
Cenumerable
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'value' in the descriptor.
Omitting the 'value' key causing undefined property.
Confusing writable with value.
4fill in blank
hard

Fill both blanks to create an object with a prototype and add a method that returns a greeting.

Javascript
const proto = {
  [1]() {
    return 'Hello, ' + this.name;
  }
};
const obj = Object.create(proto);
obj.name = 'Alice';
console.log(obj.[2]());
Drag options to blanks, or click blank then click option'
Agreet
BsayHello
Chello
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in definition and call.
Choosing method names that are not consistent.
Forgetting to define the method on the prototype.
5fill in blank
hard

Fill all three blanks to create an object with a prototype, add a property with descriptor, and call a method.

Javascript
const proto = {
  [1]() {
    return `Age is ${this.age}`;
  }
};
const obj = Object.create(proto, {
  age: { [2]: 30, writable: true, enumerable: true }
});
console.log(obj.[3]());
Drag options to blanks, or click blank then click option'
AgetAge
Bvalue
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in definition and call.
Using incorrect keys in property descriptor.
Forgetting to make the property enumerable.