Bird
Raised Fist0
Javascriptprogramming~10 mins

Prototype chain in Javascript - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does the prototype chain in JavaScript help with?
easy
A. Sharing properties and methods between objects
B. Creating new variables automatically
C. Running code faster
D. Stopping code execution

Solution

  1. Step 1: Understand prototype chain purpose

    The prototype chain allows objects to share properties and methods by linking them.
  2. Step 2: Identify correct description

    Sharing properties and methods matches the prototype chain's role, unlike other options.
  3. Final Answer:

    Sharing properties and methods between objects -> Option A
  4. Quick Check:

    Prototype chain = Sharing properties and methods [OK]
Hint: Prototype chain shares properties/methods between objects [OK]
Common Mistakes:
  • Thinking it creates variables
  • Assuming it speeds up code
  • Believing it stops code
2. Which of the following is the correct way to create an object with a specific prototype in JavaScript?
easy
A. let obj = prototypeObj.create();
B. let obj = Object.create(prototypeObj);
C. let obj = new Object(prototypeObj);
D. let obj = Object.prototype(prototypeObj);

Solution

  1. Step 1: Recall Object.create syntax

    Object.create(prototypeObj) creates a new object with prototypeObj as its prototype.
  2. Step 2: Check other options

    Other options use incorrect syntax or methods that don't exist.
  3. Final Answer:

    let obj = Object.create(prototypeObj); -> Option B
  4. Quick Check:

    Object.create() creates object with prototype [OK]
Hint: Use Object.create(prototype) to set prototype correctly [OK]
Common Mistakes:
  • Using new Object() with prototype argument
  • Calling create() on prototype object
  • Trying to call Object.prototype as a function
3. What will be the output of this code?
const parent = { greet() { return 'Hello'; } };
const child = Object.create(parent);
console.log(child.greet());
medium
A. undefined
B. null
C. TypeError
D. 'Hello'

Solution

  1. Step 1: Understand prototype lookup

    child has no greet method, so JavaScript looks up to parent where greet exists.
  2. Step 2: Call greet method from parent

    Calling child.greet() runs parent.greet(), returning 'Hello'.
  3. Final Answer:

    'Hello' -> Option D
  4. Quick Check:

    Prototype lookup finds greet() = 'Hello' [OK]
Hint: If property missing, JS checks prototype chain [OK]
Common Mistakes:
  • Expecting undefined because child lacks greet
  • Thinking it causes error
  • Assuming null is returned
4. Find the error in this code snippet:
const animal = { eats: true };
const rabbit = Object.create(animal);
rabbit.eats = false;
console.log(rabbit.eats);
console.log(animal.eats);
medium
A. No error; output is as expected
B. Object.create should not be used here
C. rabbit.eats does not override animal.eats
D. animal.eats is changed to false

Solution

  1. Step 1: Understand property shadowing

    Setting rabbit.eats creates a new property on rabbit, shadowing animal.eats.
  2. Step 2: Check outputs

    rabbit.eats logs false; animal.eats remains true. This is expected behavior.
  3. Final Answer:

    No error; output is as expected -> Option A
  4. Quick Check:

    Property shadowing works correctly [OK]
Hint: Setting property on child shadows parent's property [OK]
Common Mistakes:
  • Thinking parent's property changes
  • Believing Object.create is wrong here
  • Expecting error from property override
5. You want to create a chain where child inherits from parent, and parent inherits from grandparent. Which code correctly sets this prototype chain?
hard
A. const grandparent = {}; const parent = Object.create(child); const child = Object.create(parent);
B. const grandparent = {}; const parent = {}; const child = Object.create(parent); child.__proto__ = grandparent;
C. const grandparent = {}; const parent = Object.create(grandparent); const child = Object.create(parent);
D. const grandparent = {}; const parent = Object.create(grandparent); const child = Object.create(grandparent);

Solution

  1. Step 1: Set parent prototype to grandparent

    Using Object.create(grandparent) for parent sets grandparent as prototype.
  2. Step 2: Set child prototype to parent

    Using Object.create(parent) for child sets parent as prototype, completing chain.
  3. Step 3: Check other options

    B incorrectly overrides child's prototype to grandparent using __proto__, skipping parent; C creates a circular reference (child not defined when creating parent); D sets child directly to grandparent, skipping parent.
  4. Final Answer:

    const grandparent = {}; const parent = Object.create(grandparent); const child = Object.create(parent); -> Option C
  5. Quick Check:

    Prototype chain: child -> parent -> grandparent [OK]
Hint: Use Object.create in order from top to bottom [OK]
Common Mistakes:
  • Setting child prototype to grandparent directly
  • Reversing prototype chain order
  • Using __proto__ instead of Object.create
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard