Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a Javascript Class inherits another (without creating an obj)?

Eg:

function A(){} function B(){} B.prototype = new A(); 

How can I check if the class B inherits class A?

like image 487
Simon Avatar asked Jan 23 '13 17:01

Simon


People also ask

How do you inherit a class in JavaScript?

By calling the super() method in the constructor method, we call the parent's constructor method and gets access to the parent's properties and methods. Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.

Is there inheritance in JavaScript?

When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype.


1 Answers

Try the following:

ChildClass.prototype instanceof ParentClass 
like image 103
Nirvana Tikku Avatar answered Oct 09 '22 18:10

Nirvana Tikku