📖 Scenario: Imagine you have a simple object representing a car. You want to understand how JavaScript looks up properties through the prototype chain when you try to access a property that is not directly on the object.
🎯 Goal: You will create an object with a prototype, add properties to both, and then access properties to see how the prototype chain works.
📋 What You'll Learn
Create an object called car with a property make set to 'Toyota'
Create an object called vehicle with a property wheels set to 4
Set vehicle as the prototype of car
Access and print the make property from car
Access and print the wheels property from car to show prototype chain lookup
💡 Why This Matters
🌍 Real World
Understanding prototype chains helps you work with JavaScript objects and inheritance, which is common in web development and frameworks.
💼 Career
Many JavaScript jobs require knowledge of prototypes to debug code, optimize performance, and understand how libraries and frameworks work under the hood.
Progress0 / 4 steps
1
Create the car object
Create an object called car with a property make set to 'Toyota'.
Javascript
Hint
Use const car = { make: 'Toyota' }; to create the object.
2
Create the vehicle object
Create an object called vehicle with a property wheels set to 4.
Javascript
Hint
Use const vehicle = { wheels: 4 }; to create the object.
3
Set vehicle as the prototype of car
Use Object.setPrototypeOf to set vehicle as the prototype of car.
Javascript
Hint
Use Object.setPrototypeOf(car, vehicle); to link the prototype.
4
Access and print properties to see prototype chain
Print the make property from car and then print the wheels property from car to show prototype chain lookup.
Javascript
Hint
Use console.log(car.make); and console.log(car.wheels); to print the properties.
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
Step 1: Understand prototype chain purpose
The prototype chain allows objects to share properties and methods by linking them.
Step 2: Identify correct description
Sharing properties and methods matches the prototype chain's role, unlike other options.
Final Answer:
Sharing properties and methods between objects -> Option A
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
Step 1: Recall Object.create syntax
Object.create(prototypeObj) creates a new object with prototypeObj as its prototype.
Step 2: Check other options
Other options use incorrect syntax or methods that don't exist.
Final Answer:
let obj = Object.create(prototypeObj); -> Option B
Quick Check:
Object.create() creates object with prototype [OK]
Hint: Use Object.create(prototype) to set prototype correctly [OK]
Using Object.create(grandparent) for parent sets grandparent as prototype.
Step 2: Set child prototype to parent
Using Object.create(parent) for child sets parent as prototype, completing chain.
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.