0
0
Javascriptprogramming~10 mins

Prototype inheritance in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Prototype inheritance
Create object A
Assign prototype of A to object B
Access property on B
Check if property exists on B
Return property
Return property from A if found
If not found, return undefined
When accessing a property on an object, JavaScript looks first on the object itself. If not found, it looks up the prototype chain until it finds the property or returns undefined.
Execution Sample
Javascript
const parent = {a: 1};
const child = Object.create(parent);
child.b = 2;
console.log(child.a);
console.log(child.b);
This code creates a child object inheriting from parent and accesses properties from both child and parent.
Execution Table
StepActionObject AccessedProperty Looked UpFound OnValue Returned
1Create parent objectparent---
2Create child with prototype parentchild---
3Assign property b=2 on childchildbchild2
4Access child.achildaparent1
5Access child.bchildbchild2
💡 Property lookup stops when property is found or prototype chain ends.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
parent{a:1}{a:1}{a:1}{a:1}
childundefinedprototype -> parentprototype -> parent, b=2prototype -> parent, b=2
Key Moments - 3 Insights
Why does child.a return 1 even though 'a' is not directly on child?
Because child inherits from parent via prototype. The lookup finds 'a' on parent as shown in execution_table step 4.
What happens if a property is not found on child or parent?
The lookup continues up the prototype chain until no prototype exists, then returns undefined. This is implied in the exit_note.
Does assigning child.b = 2 affect parent?
No, child.b is a direct property on child only, parent remains unchanged as shown in variable_tracker after step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 4, where is property 'a' found when accessed on child?
AOn parent
BOn child
CUndefined
DOn global object
💡 Hint
Check the 'Found On' column in step 4 of execution_table.
At which step is property 'b' assigned directly to child?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for property assignments.
If we tried to access child.c which does not exist, what would happen?
AReturn value from parent if exists
BThrow an error
CReturn undefined
DReturn null
💡 Hint
Refer to the exit_note about property lookup stopping when property is not found.
Concept Snapshot
Prototype inheritance in JavaScript:
- Objects can inherit properties via prototype chain.
- Accessing a property checks own properties first.
- If not found, lookup continues up prototype chain.
- If property not found anywhere, returns undefined.
- Use Object.create(proto) to set prototype.
Full Transcript
Prototype inheritance means when you ask for a property on an object, JavaScript first looks on that object. If it doesn't find it, it looks on the object's prototype. This continues up the chain until the property is found or there are no more prototypes. In the example, child inherits from parent. So child.b is found directly on child, but child.a is found on parent. If a property is missing on both, the result is undefined.