Write a function `getPrototypeChain(obj)` that returns an array of constructor names in the prototype chain.
Problem Statement
Write a function `getPrototypeChain(obj)` that returns an array of constructor names in the prototype chain.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Object.getPrototypeOf(obj) returns the prototype of obj.
š” Hint 2:
Keep traversing until you reach null.
š” Hint 3:
Push constructor.name for each prototype that has a constructor.
ā
Solution:
```javascript
function getPrototypeChain(obj) {
const chain = [];
let proto = Object.getPrototypeOf(obj);
while (proto) {
if (proto.constructor) chain.push(proto.constructor.name);
proto = Object.getPrototypeOf(proto);
}
return chain;
}
```