0

I'm currently trying to access the method of a specific ancestor in my dependency chain. Specifically, I want to run ParentClass.logName() from my initialized GrandChildClass. This is how I access the ParentClass prototype (approach taken from Get parent class name from child with ES6?).

class AncestorClass {
    constructor (name, id){
        this.name = name + id;
    }
    logName () {
        console.log(this.name);
    }
    
}

class ParentClass extends AncestorClass {
    constructor (name, id) {
        super(name, 1);
        this.name = name + id;
    }
}

class ChildClass extends ParentClass {
    constructor (name, id) {
        super(name, 2);
        this.name = name + id;
    }
}

class GrandchildClass extends ChildClass {
    constructor (name, id){
        super(name, 3);
        this.name = name + id;
    }
}

const grandchild = new GrandchildClass('testName', 4);
const parent =  Object.getPrototypeOf((Object.getPrototypeOf(grandchild.constructor)));
console.log(parent);
const initParent = new parent();
initParent.logName();

So I got the prototype and initialized it with the new keyword but for some reason initParent.logName() returns NaN.

So how do I call to ParentClass.logName from my grandchild without initializing ParentClass directly?

Tom M
  • 2,815
  • 2
  • 20
  • 47
  • 1
    Just call `grandchild.logName()` ? Not sure I understand what the problem is. In general, you never want to access a method of a *specific* ancestor, because the calling code should *never* know where exactly the method is defined. All it needs to know is that the object conforms to an interface. If `grandchild.logName()` doesn't solve your problem then you have to provide more information about what you are trying to achieve. – Felix Kling Aug 24 '17 at 14:35
  • `grandchild.logName()` works... – kukkuz Aug 24 '17 at 14:35
  • ParentClass.prototype.logName.call(instanceOfGrandChild); – Jerinaw Aug 24 '17 at 14:36
  • Also check this out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super – Jerinaw Aug 24 '17 at 14:38

1 Answers1

2

You initialize parent with an empty constructor, which means both attribute are undefined and undefined + undefined gives NaN.

const initParent = new parent('foo', 1);
initParent.logName() // print 'foo1'

class AncestorClass {
    constructor (name, id){
        this.name = name + id;
    }
    logName () {
        console.log(this.name);
    }
    
}

class ParentClass extends AncestorClass {
    constructor (name, id) {
        super(name, 1);
        this.name = name + id;
    }
}

class ChildClass extends ParentClass {
    constructor (name, id) {
        super(name, 2);
        this.name = name + id;
    }
}

class GrandchildClass extends ChildClass {
    constructor (name, id){
        super(name, 3);
        this.name = name + id;
    }
}

const grandchild = new GrandchildClass('testName', 4);
const parent =  Object.getPrototypeOf((Object.getPrototypeOf(grandchild.constructor)));
console.log(parent);
const initParent = new parent('foo', 1);
initParent.logName();
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82