7

I want to create a method in a parent class that will return the location of each subclass that extends it.

// Class A dir is '/classes/a/
class A{      
    getPath(){
        console.log(__dirname);
        return (__dirname);
    }
}

// Class B dir is '/classes/b'
class B extends A{ 

}

new A().getPath(); // Prints '/classes/a'
new B().getPath(); // Prints '/classes/a' (I want here '/classes/b');

I understand why class B prints the location of A, but I'm looking for an option of making a method on the parent class that will print the subclass's location. I don't want to override the method in every subclass because it missing the point

I tried process.pwd() as well.

Rami Loiferman
  • 853
  • 1
  • 6
  • 22
  • Related: http://stackoverflow.com/questions/13227489/how-can-one-get-the-file-path-of-the-caller-function-in-node-js – Jordan Running Jan 18 '17 at 07:16
  • Calling `new B().getPath()` will actually execute `A.prototype.getPath.call(new B())`, if you don't overwrite the method in B(which creates `B.prototype.getPath` in object B's prototype chain), obviously the `__dirname` will remain as A's dirname. – Allen Jan 18 '17 at 07:20
  • Isn't there some other option instead of __dirname? that will return B's dir ? – Rami Loiferman Jan 18 '17 at 09:14
  • @RamiLoiferman A class doesn't have a location. Only a file has a location. You can't "return B's dir" because B doesn't have any such concept. Once the code for a class is parsed and evaluated, the class lives in the JavaScript engine, in memory, not in a file. – Jordan Running Jan 18 '17 at 14:26
  • Have you tried using https://nodejs.org/docs/latest/api/path.html#path_path_dirname_path – isnvi23h4 Jan 20 '17 at 08:26

3 Answers3

1

If you are into slightly more hacky approaches, which might be required to be updated in the future, you can retrieve the path of the subclass quite easily. You have your class that extends another class (or a class that does not extend anything for that matter):

module.exports = class B extends A
{ 
    // TODO: implement
}

In order to retrieve the path of that class, we can do the following:

/* For this method we need to have the constructor function itself. Let us pretend that this
 * was required by a user using our framework and we have no way of knowing what it actually 
 * is from the framework itself.
 */
let B = require('path-to-b')
let path = require('path')
let dirOfB

for (let [, val] of require('module')._cache)
    if (val.exports === B)
        dirOfB = path.dirname(val.filename)

console.log(dirOfB)

// Expected output: path-to-b

This approach, even though slightly hacky, works well if we have the constructor function, but are unsure of the directory it actually lies within.

D. Ataro
  • 1,711
  • 17
  • 38
0

Use instance variables

// Class A dir is '/classes/a/
class A{    
    __dirname = __dirname;
  
    getPath(){
        return this.__dirname;
    }
}

// Class B dir is '/classes/b'
class B extends A{
  __dirname = __dirname;
}

new A().getPath(); // Prints '/classes/a'
new B().getPath(); // Prints '/classes/a' (I want here '/classes/b');
Yusufbek
  • 2,180
  • 1
  • 17
  • 23
-1

You can do with overriding the method only. If you don't want this than you should not be using inheritance since this is based on inheritance definition.

Volem
  • 616
  • 3
  • 15