Prototype inheritance in Javascript - Time & Space Complexity
When using prototype inheritance in JavaScript, it's important to understand how looking up properties works under the hood.
We want to know how the time to find a property changes as the prototype chain grows longer.
Analyze the time complexity of property access using prototype inheritance.
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
return `Hello, ${this.name}`;
};
const alice = new Person('Alice');
console.log(alice.greet());
This code creates an object with a prototype method and accesses that method through the prototype chain.
When accessing alice.greet(), JavaScript looks for greet on the object and its prototypes.
- Primary operation: Checking each prototype object for the property.
- How many times: Up to the length of the prototype chain until the property is found.
As the prototype chain gets longer, the number of checks to find a property grows.
| Prototype Chain Length (n) | Approx. Property Checks |
|---|---|
| 1 | 1 |
| 10 | Up to 10 |
| 100 | Up to 100 |
Pattern observation: The time to find a property grows linearly with the prototype chain length.
Time Complexity: O(n)
This means the time to access a property depends on how many prototypes JavaScript must check in a row.
[X] Wrong: "Accessing a property on an object is always instant no matter how long the prototype chain is."
[OK] Correct: JavaScript must check each prototype one by one until it finds the property or reaches the end, so longer chains take more time.
Understanding prototype inheritance time helps you explain how JavaScript objects work and why deep prototype chains can slow down property access.
"What if we cache the property on the object after the first lookup? How would that affect the time complexity of future accesses?"
Practice
Solution
Step 1: Understand prototype inheritance concept
Prototype inheritance allows one object to use properties and methods from another object.Step 2: Match concept to options
Only Objects to share properties and methods correctly describes sharing properties and methods between objects.Final Answer:
Objects to share properties and methods -> Option DQuick Check:
Prototype inheritance = share properties and methods [OK]
- Thinking it speeds up functions
- Confusing with variable privacy
- Assuming it limits array types
parentObj?Solution
Step 1: Recall correct syntax for prototype inheritance
UseObject.create(parentObj)to create a new object inheriting fromparentObj.Step 2: Check options for correct method
Only const child = Object.create(parentObj); usesObject.createcorrectly; others are invalid or incorrect.Final Answer:
const child = Object.create(parentObj); -> Option CQuick Check:
Object.create() creates inherited object [OK]
- Using new Object(parentObj) which doesn't inherit
- Using non-existent Object.inherit method
- Calling create on parentObj instead of Object
function Parent() { this.name = 'Parent'; }
Parent.prototype.greet = function() { return 'Hello from ' + this.name; };
function Child() { Parent.call(this); this.name = 'Child'; }
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
const c = new Child();
console.log(c.greet());Solution
Step 1: Understand property assignment in Child constructor
Child calls Parent constructor to setthis.name, then overwrites it with 'Child'.Step 2: Check prototype chain and method call
Child's prototype inherits from Parent.prototype, sogreet()is available and usesthis.namewhich is 'Child'.Final Answer:
'Hello from Child' -> Option BQuick Check:
Child instance uses own name 'Child' in greet() [OK]
- Assuming greet uses Parent's name
- Forgetting to call Parent.call(this)
- Not resetting Child.prototype.constructor
function Animal() { this.alive = true; }
Animal.prototype.breathe = function() { return 'breathing'; };
function Dog() { Animal.call(this); this.bark = function() { return 'woof'; } }
Dog.prototype = Animal.prototype;
const d = new Dog();
console.log(d.breathe());Solution
Step 1: Identify prototype assignment mistake
AssigningDog.prototype = Animal.prototypelinks both constructors to same prototype object, causing shared mutations.Step 2: Correct way to inherit prototype
UseDog.prototype = Object.create(Animal.prototype)to create a new object inheriting from Animal.prototype without sharing it directly.Final Answer:
Dog.prototype should be Object.create(Animal.prototype) -> Option AQuick Check:
Use Object.create for prototype inheritance [OK]
- Assigning prototype directly instead of using Object.create
- Confusing call() usage
- Defining methods inside constructor unnecessarily
Car object that inherits from Vehicle and adds a drive method. Which code correctly sets this up and calls drive on a Car instance?Solution
Step 1: Set up inheritance correctly
UseVehicle.call(this)inside Car constructor to copy properties, then setCar.prototype = Object.create(Vehicle.prototype)to inherit methods.Step 2: Add drive method and create instance
Definedriveon Car.prototype, then create instance and calldrive().Final Answer:
Code in option A correctly sets inheritance and calls drive() -> Option AQuick Check:
Call parent, use Object.create, add method, then call [OK]
- Assigning prototype directly instead of Object.create
- Not calling Vehicle.call(this) in Car
- Forgetting to set constructor property
- Defining methods before setting prototype
Ready to prove it?
Pass the test and this pattern moves unknown
