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

es6 class - Javascript ES6: How to retrieve calling subclass from a static method defined in superclass - Stack Overflow

programmeradmin4浏览0评论

New to JavaScript.

Seeking some guidance on how to access the calling class name from a static method defined in the superclass using ES6 classes. I've spent an hour searching, but have not been able to e up with a solution.

A code snippet may help clarify what I am seeking

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 illustrated above, I can easily get the subclass name from a instance. Not quite sure how to access from a static method.

Ideas for the implementation of static get callingClassType() weled.

New to JavaScript.

Seeking some guidance on how to access the calling class name from a static method defined in the superclass using ES6 classes. I've spent an hour searching, but have not been able to e up with a solution.

A code snippet may help clarify what I am seeking

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 illustrated above, I can easily get the subclass name from a instance. Not quite sure how to access from a static method.

Ideas for the implementation of static get callingClassType() weled.

Share Improve this question asked May 21, 2017 at 4:04 So Over ItSo Over It 3,6983 gold badges36 silver badges46 bronze badges 7
  • You'll be hard pressed to find a way to do that in any language. Because static method means no this, so there is no notion of "the calling instance". – Kevin Commented May 21, 2017 at 4:08
  • Try return SuperClass.constructor.name; – Derek Commented May 21, 2017 at 4:11
  • @Derek, that returns Function, not SubClass. Is there any way to inspect the call chain and pull out from there? – So Over It Commented May 21, 2017 at 4:17
  • @Kevin I do get there is no calling instance. I was wondering if there was some way to inspect the chain. – So Over It Commented May 21, 2017 at 4:18
  • 2 @Kevin Actually it's pretty easy in PHP: get_called_class(). And as Felix's answer shows below it's doable in JS too. – Matt Browne Commented May 21, 2017 at 4:45
 |  Show 2 more ments

2 Answers 2

Reset to default 8

callingClassType is a function (well, a getter in this case, same thing). The value of this inside a function depends on how it is called. If you call a function with foo.bar(), then this inside bar will refer to foo.

Thus if you "call" the function with SubClass.callingClassType, this will refer to SubClass. SubClass is itself a (constructor) function thus you can get its name via the name property.

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)

Have a look at the MDN documentation to learn more about this.

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' 

From https://developer.mozilla/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's instantiation)

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论