Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Super Class In JavaScript

Tags:

javascript

I am making a class.

class A {
    constructor() {
    }
    getThis() {
        return this;
    }
}
class B extends A {
    constructor() {
        super();

        this.superclass = super;  // SyntaxError: 'super' keyword unexpected here
        this.superclass = super.getThis();  // this.superclass is this(B and not A)
    }
}

How can I access the super class (not the property or methods)?


Edit:

class A {
    constructor() {
        this.a = 1;
    }
    getThis() {
        return this;
    }
}
class B extends A {
    constructor() {
        super();
        this.b = 2;

        console.log( [get the super class] );  // A { a: 1 } 
        console.log(this);  // B { a: 1, b: 2 }
    }
}

Is it possible to get the super class with the data?

like image 610
Und Der Avatar asked Feb 20 '21 10:02

Und Der


People also ask

What is super (); in JavaScript?

The super keyword in JavaScript acts as a reference variable to the parent class. It is mainly used when we want to access a variable, method, or constructor in the base class from the derived class.

Is super () necessary JavaScript?

The fish class doesn't need super() because its “parent” is just the JavaScript Object. Fish is already at the top of the prototypal inheritance chain, so calling super() is not necessary — fish's this context only needs to include Object, which JavaScript already knows about.

What does super () __ Init__ do?

Understanding Python super() with __init__() methods It is known as a constructor in Object-Oriented terminology. This method when called, allows the class to initialize the attributes of the class. The super() function allows us to avoid using the base class name explicitly.


Video Answer


2 Answers

It's rare that doing so is of any use to you, but there are a few ways when using class syntax:

You can get the prototype of B:

Object.getPrototypeOf(B) === A // true

That works because class syntax assigns the A constructor function as the prototype of the B constructor function (which is handy, it means B inherits static methods from A).

Or you can do it by getting the constructor property of the prototype of B.prototype:

Object.getPrototypeOf(B.prototype).constructor === A // true

Or if you want to use an isntance of B as your starting point (this in this example):

Object.getPrototypeOf(Object.getPrototypeOf(this)).constructor === A // true

Live Example:

class A {
    constructor() {
    }
}
class B extends A {
    constructor() {
        super();
        
        console.log(Object.getPrototypeOf(B) === A); // true

        console.log(Object.getPrototypeOf(B.prototype).constructor === A); // true

        // Or to get it from `this`:
        console.log(Object.getPrototypeOf(Object.getPrototypeOf(this)).constructor === A); // true
    }
}

new B();

Your getThis suggests a misconception, though. When you do new B to create an instance of B, there is not a separate object created that's just an instance of A. The one object that new B creates is a combination of the features of A and B as a result of both inheritance and initialization. There is no separate A instance. (There is A.prototype, but that isn't an instance of A, it's just an object used as the prototype of instances of A.)


Re your edit:

Is it possible to get the super class with the data?

I think you mean "Is it possible to get the instance of the super class with the data?" The answer is yes or no depending on how you want to look at it. An instance of B is an instance of A, so in that sense, "yes" because you already have it because you have an instance of B. But there is no separate instance of A that's only an A and not a B (see above), so in that sense, "no."

With the A and B in your edit, before doing new B, you have something like this in memory, basically the two constructor functions A and B and their associated A.prototype and B.prototype objects:

               +−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
               |                                                       |
               v                                                       |
       +−−−−−−−−−−−−−−−+                                               |
A−−−−−>| Function A    |  +−>Function.prototype                        |
       +−−−−−−−−−−−−−−−+  |                                            |
       | [[Prototype]] |−−+                                            |
       | name: "A"     |     +−−−−−−−−−−−−−−−+                         |
       | prototype     |−−−−>| A.prototype   |                         |
       +−−−−−−−−−−−−−−−+     +−−−−−−−−−−−−−−−+                         |
               ^             | [[Prototype]] |−−−−−>Object.prototype   |
               |             | constructor   |−−−−−−−−−−−−−−−−−−−−−−−−−+
               |             +−−−−−−−−−−−−−−−+
               +−−−−−−−−−−+          ^
                          |          |
       +−−−−−−−−−−−−−−−+  |          +−−−−−−−−−−+
B−−−−−>| Function B    |  |                     |
       +−−−−−−−−−−−−−−−+  |                     |
       | [[Prototype]] |−−+                     |
       | name: "B"     |     +−−−−−−−−−−−−−−−+  |
       | prototype     |−−−−>| B.prototype   |  |
       +−−−−−−−−−−−−−−−+     +−−−−−−−−−−−−−−−+  |
               ^             | [[Prototype]] |−−+
               |             | constructor   |−−+
               |             +−−−−−−−−−−−−−−−+  |
               |                                |
               +−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+

(Some details omitted.)

Now, if you do:

const b = new B();

that creates a single object. The A constructor adds an a property to that new object, and the B constructor adds a b property to that new object:

       +−−−−−−−−−−−−−−−+
b−−−−−>| Instance of B |
       +−−−−−−−−−−−−−−−+
       | [[Prototype]] |−−−−>B.prototype
       | a: 1          |
       | b: 2          |
       +−−−−−−−−−−−−−−−+

It's a single object, not separate ones containing a and b. The this that A's code uses in this.a = 1 is the same object as the this that B's code uses in this.b = 2.

like image 121
T.J. Crowder Avatar answered Nov 12 '22 13:11

T.J. Crowder


What you're asking shouldn't be necessary, I hope this answer can clarify that a bit

Here we see that you never need return this because a class constructor already returns itself by default:

class A {
    constructor(){
        // already returns this to the caller of `new`
    }
}

let a = new A()
console.log(a)   // logs the `this` scope of the A instance

If B extends A, then the resulting object contains the properties and methods of A and B combined. You can check that by doing console.log(b) . There is no A instance.

If B overwrites methods of A - making the original A method invisible, then you can still find them by using super...

class A {
   doSomething(){
     console.log("world")
   }
}

class B extends A {
  constructor() {
    super()
  }
  doSomething(){
    console.log("hello")
    super.doSomething()
  }
}

let b = new B()
b.doSomething() // logs: hello world

Not an exact answer to your question but maybe it's helpful anyway!

like image 29
Kokodoko Avatar answered Nov 12 '22 14:11

Kokodoko