Complete the code to create an object that inherits from another object using __proto__.
const parent = { greet() { return 'Hello'; } };
const child = { __proto__: [1] };
console.log(child.greet());The __proto__ property sets the prototype of the object. Setting child.__proto__ = parent makes child inherit from parent.
Complete the code to check if 'greet' is in the prototype chain of child.
const parent = { greet() { return 'Hi'; } };
const child = { __proto__: parent };
console.log([1] in child);The in operator checks if a property exists in the object or its prototype chain. The property name must be a string.
Fix the error in the code to correctly set the prototype of child to parent using Object.setPrototypeOf.
const parent = { say() { return 'Hey'; } };
const child = {};
Object.setPrototypeOf(child, [1]);
console.log(child.say());Object.setPrototypeOf(obj, prototype) sets the prototype of obj. Here, child should inherit from parent.
Fill both blanks to create a prototype chain where grandChild inherits from child, and child inherits from parent.
const parent = { speak() { return 'Hello'; } };
const child = { __proto__: [1] };
const grandChild = { __proto__: [2] };
console.log(grandChild.speak());Setting child.__proto__ = parent and grandChild.__proto__ = child creates a chain where grandChild inherits from child, which inherits from parent.
Fill all three blanks to create a prototype chain and check if 'speak' is accessible from grandChild.
const parent = { speak() { return 'Hi'; } };
const child = { __proto__: [1] };
const grandChild = { __proto__: [2] };
console.log([3] in grandChild);The prototype chain is set by linking child to parent and grandChild to child. The in operator checks if 'speak' exists in grandChild or its prototypes.