Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
In Javascript, upon passing a declared class as an argument to Object.getPrototypeOf, the result refers to the parent class. Why is that?

For instance :

class Parent {

  foo() {
    console.log("foo");
  }
}

class Child extends Parent {

  bar() {
    console.log("bar");
  }
}

Here, the expression

Object.getPrototypeOf(Child) === Parent

would evaluate to true.

Now I understand the basics of prototypical inheritance and how classes work in Javascript that is, every function has a prototype property, which is copied on to the newly created class instance's __proto__ property upon calling the function with the new operator

I also understand that Object.getPrototypeOf essentially returns the __proto__ property of any given object and generally we simulate inheritance in pure javascript by the means of setting the prototype property of the child class to an instance of the parent class, like this :

function Parent(){}
 
Parent.prototype.foo = function(){
  console.log("foo");
}
 
function Child(){}
 
// Inherit properties from Parent
Child.prototype = new Parent();

My questions are :

  • what does the result of Object.getPrototypeOf when called with a class as an argument represent ? How is it linked to class inheritance in TS/JS ?

  • Is it safe to use Object.getPrototypeOf to check if whether a particular class extends another class at runtime ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The extends keyword does two things:

  • It sets the SuperClass.prototype object as the prototype of the SubClass.prototype object. In your case, Child.prototype gets Parent.prototype object as its prototype.

  • It also sets the super class' constructor as the prototype of the child class constructor. In your case, Child constructor gets Parent constructors as its prototype.

Above two statements essentially mean the following in code:

class Parent {}
class Child extends Parent {}

console.log(Object.getPrototypeOf(Child.prototype) === Parent.prototype);
console.log(Object.getPrototypeOf(Child) === Parent);

In other words, the extends keyword sets up two prototype chains:

Child.prototype ---> Parent.prototype ---> Object.prototype

Child ---> Parent ---> Function.prototype ---> Object.prototype

So, Object.getPrototypeOf(Child) simply returns the Parent constructor.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!