0
0
Javascriptprogramming~10 mins

Prototype chain 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 an object that inherits from another object using __proto__.

Javascript
const parent = { greet() { return 'Hello'; } };
const child = { __proto__: [1] };
console.log(child.greet());
Drag options to blanks, or click blank then click option'
AObject
Bchild
Cparent
D{}
Attempts:
3 left
💡 Hint
Common Mistakes
Using the child object itself as prototype causes no inheritance.
Using Object or {} does not link to the parent object.
2fill in blank
medium

Complete the code to check if 'greet' is in the prototype chain of child.

Javascript
const parent = { greet() { return 'Hi'; } };
const child = { __proto__: parent };
console.log([1] in child);
Drag options to blanks, or click blank then click option'
A'parent'
Bgreet
C'child'
D'greet'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the property name without quotes causes a ReferenceError.
Checking for 'child' or 'parent' as strings is incorrect.
3fill in blank
hard

Fix the error in the code to correctly set the prototype of child to parent using Object.setPrototypeOf.

Javascript
const parent = { say() { return 'Hey'; } };
const child = {};
Object.setPrototypeOf(child, [1]);
console.log(child.say());
Drag options to blanks, or click blank then click option'
Aparent
Bchild
C{}
DObject
Attempts:
3 left
💡 Hint
Common Mistakes
Passing child as the prototype causes no inheritance.
Passing {} or Object is incorrect.
4fill in blank
hard

Fill both blanks to create a prototype chain where grandChild inherits from child, and child inherits from parent.

Javascript
const parent = { speak() { return 'Hello'; } };
const child = { __proto__: [1] };
const grandChild = { __proto__: [2] };
console.log(grandChild.speak());
Drag options to blanks, or click blank then click option'
Aparent
Bchild
C{}
DgrandChild
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of prototypes.
Setting prototypes to empty objects breaks the chain.
5fill in blank
hard

Fill all three blanks to create a prototype chain and check if 'speak' is accessible from grandChild.

Javascript
const parent = { speak() { return 'Hi'; } };
const child = { __proto__: [1] };
const grandChild = { __proto__: [2] };
console.log([3] in grandChild);
Drag options to blanks, or click blank then click option'
Aparent
Bchild
C'speak'
D'child'
Attempts:
3 left
💡 Hint
Common Mistakes
Using property names without quotes in 'in' operator.
Setting prototypes incorrectly breaks the chain.