Complete the code to create a new object with the specified prototype.
const proto = { greet() { return 'Hello'; } };
const obj = Object.[1](proto);
console.log(obj.greet());The Object.create method creates a new object with the given prototype object.
Complete the code to add a property to the new object created with Object.create.
const proto = { type: 'animal' };
const obj = Object.create(proto);
obj.[1] = 'dog';
console.log(obj.type, obj.name);The property name is added directly to the new object, while type comes from the prototype.
Fix the error in the code to properly create an object with a prototype and a property descriptor.
const proto = { speak() { return 'Hi'; } };
const obj = Object.create(proto, {
age: { [1]: 5, writable: true }
});
console.log(obj.age, obj.speak());The property descriptor must use value to set the property's value.
Fill both blanks to create an object with a prototype and add a method that returns a greeting.
const proto = {
[1]() {
return 'Hello, ' + this.name;
}
};
const obj = Object.create(proto);
obj.name = 'Alice';
console.log(obj.[2]());The method name must be the same when defined and called. Here, 'greet' is used consistently.
Fill all three blanks to create an object with a prototype, add a property with descriptor, and call a method.
const proto = {
[1]() {
return `Age is ${this.age}`;
}
};
const obj = Object.create(proto, {
age: { [2]: 30, writable: true, enumerable: true }
});
console.log(obj.[3]());The method is named 'getAge' and is called as such. The property descriptor uses 'value' to set the age.