最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get Super Class In JavaScript - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Feb 20, 2021 at 20:23 Anonymous 7544 gold badges15 silver badges41 bronze badges asked Feb 20, 2021 at 10:43 Und DerUnd Der 836 bronze badges 2
  • Re your edit: See the second and third parts of my answer. When you do new B, there is no separate instance of A created; just one object that's both an instance of B and an instance of A is created. – T.J. Crowder Commented Feb 20, 2021 at 10:57
  • 1 Do you just want to print out the name of the super class? what's your goal? – Riley Commented Mar 18, 2021 at 1:01
Add a ment  | 

2 Answers 2

Reset to default 10

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 instance 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 bination 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.

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 bined. 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!

发布评论

评论列表(0)

  1. 暂无评论