Javascript ES6: how to get subclass call from static method defined in superclass

New to JavaScript.

Looking for some guidance on how to access the called class name from a static method defined in a superclass using ES6 classes. I spent an hour searching but couldn't find a solution.

The code snippet might help clarify what I am looking for.

class SuperClass {
    get callingInstanceType() { return this.constructor.name }
    static get callingClassType() { return '....help here ...' }
}

class SubClass extends SuperClass { }

let sc = new SubClass()

console.log(sc.callingInstanceType)     // correctly prints 'SubClass'
console.log(SubClass.callingClassType)  // hoping to print 'SubClass'

      

As shown above, I can easily get the subclass name from the instance. Not really sure how to access the static method.

Ideas for implementation are welcome static get callingClassType()

.

+3


source to share


2 answers


callingClassType

is a function (well, the getter is the same in this case). The meaning this

inside the function depends on how it is called. If you call a function with foo.bar()

, then this

internally it bar

will refer to foo

.

Thus, if you "call" a function using SubClass.callingClassType

, it this

will refer to SubClass

. SubClass

itself is a (constructor), so you can get its name using a property name

.

So your method definition should be

static get callingClassType() { return this.name; }

      



class SuperClass {
  get callingInstanceType() {
    return this.constructor.name
  }
  static get callingClassType() {
    return this.name
  }
}

class SubClass extends SuperClass {}

let sc = new SubClass()

console.log(sc.callingInstanceType)
console.log(SubClass.callingClassType)
      

Run codeHide result


Take a look at the MDN documentation to learn more aboutthis

.

+6


source


Use SuperClass.prototype.constructor.name

:

class SuperClass {
    get callingInstanceType() { return this.constructor.name }
    static get callingClassType() { return SuperClass.prototype.constructor.name; }
}

class SubClass extends SuperClass {}
class SubClass2 extends SuperClass {
     static get callingClassType() { return SubClass2.prototype.constructor.name; }
}

console.log(SuperClass.callingClassType); // 'SuperClass'
console.log(SubClass.callingClassType); // 'SuperClass'
console.log(SubClass2.callingClassType); // 'SubClass2' 
      

Run codeHide result




From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static#Examples :

class Triple {
  static triple(n) {
    if (n === undefined) {
      n = 1;
    }
    return n * 3;
  }
}

class BiggerTriple extends Triple {
  static triple(n) {
    return super.triple(n) * super.triple(n);
  }
}

console.log(Triple.triple());        // 3
console.log(Triple.triple(6));       // 18

var tp = new Triple();

console.log(BiggerTriple.triple(3));
// 81 (not affected by parent instantiation)

console.log(tp.triple());
// 'tp.triple is not a function'.

      

+1


source







All Articles