get parent class javascript

Get Parent Class Javascript

Getting the parent class in JavaScript can be a bit tricky, especially if you are not familiar with the object-based nature of the language. However, there are a few ways to get the parent class of an object in JavaScript.

Method 1: Using Object.getPrototypeOf()

The first method to get the parent class of an object is to use the Object.getPrototypeOf() method. This method returns the prototype of the object, which is essentially the parent class.


// Define parent class
class Animal {
  constructor(name) {
    this.name = name;
  }
}

// Define child class
class Dog extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }
}

// Create instance of Dog
let myDog = new Dog("Rufus", "Golden Retriever");

// Get parent class of myDog
let parentClass = Object.getPrototypeOf(myDog).constructor;
console.log(parentClass); // Output: Animal

In the above example, we define a parent class called Animal and a child class called Dog. We create an instance of Dog called myDog and then use the Object.getPrototypeOf() method to get its parent class. We then log the parent class to the console and get "Animal" as the output.

Method 2: Using __proto__ property

The second method to get the parent class of an object is to use the __proto__ property. This property is an internal property that contains a reference to the prototype of the object.


// Define parent class
class Animal {
  constructor(name) {
    this.name = name;
  }
}

// Define child class
class Dog extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }
}

// Create instance of Dog
let myDog = new Dog("Rufus", "Golden Retriever");

// Get parent class of myDog
let parentClass = myDog.__proto__.constructor;
console.log(parentClass); // Output: Animal

In the above example, we define the same parent and child classes as before, create an instance of Dog called myDog, and then use the __proto__ property to get its parent class. We then log the parent class to the console and get "Animal" as the output.

Method 3: Using instanceof operator

The third method to get the parent class of an object is to use the instanceof operator. This operator returns true if an object is an instance of a particular class, including its parent classes.


// Define parent class
class Animal {
  constructor(name) {
    this.name = name;
  }
}

// Define child class
class Dog extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }
}

// Create instance of Dog
let myDog = new Dog("Rufus", "Golden Retriever");

// Check if myDog is an instance of Animal
if(myDog instanceof Animal) {
  console.log("myDog is an instance of Animal");
}
// Output: "myDog is an instance of Animal"

In the above example, we define the same parent and child classes as before, create an instance of Dog called myDog, and then use the instanceof operator to check if myDog is an instance of Animal. Since Animal is the parent class of Dog, myDog is also an instance of Animal.

These are the three methods to get the parent class of an object in JavaScript. Depending on your use case, one method may be more appropriate than the others.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe