0

I'm trying to use badPort function inside another function (getPort) in module.exports like so:

DEFAULT_PORT = 80;

module.exports = {
        badPort:
                (c_port, min=80, max=90) => {
                        return isNaN(c_port) || !between(c_port, min, max);
                },
        getPort:
                (c_port, min=80, max=90) => {
                        console.log(this.badPort(c_port));
                        return this.badPort(c_port) ? 80 : c_port;
                },
};     

However, when I use this.badPort(c_port) it throws an exception:

TypeError: this.badPort is not a function

But it's clearly a function as initialised right above it.

If however I take out the (), this.badPort always returns undefined.

Why is this happening and how would I be able to correctly use the function inside module.exports? Is it possible to also declare the "Global variable" DEFAULT_PORT in module.exports this way?

ludicrous
  • 175
  • 9
  • Does this answer your question? [How does the "this" keyword work?](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – ASDFGerte Mar 05 '22 at 19:12
  • @ASDFGerte so I cannot reference another function in export.modules from inside? – ludicrous Mar 05 '22 at 19:16

3 Answers3

1

You can do it, by changing this to module.exports:

DEFAULT_PORT = 80;

module.exports = {
        badPort:
                (c_port, min=80, max=90) => {
                        return isNaN(c_port) || !between(c_port, min, max);
                },
        getPort:
                (c_port, min=80, max=90) => {
                        console.log(module.exports.badPort(c_port));
                        return module.exports.badPort(c_port) ? 80 : c_port;
                },
};

And about second question... you would have to redefine your module, to use that variable externally, like this:

module.exports.DEFAULT_PORT = 80;

Then you will have access to it:

var mod = require('mymodule');
console.log(mod.DEFAULT_PORT);

I am not aware of any other way.

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
  • This works! Would it be best practice to define functions as const along the file and then add them to modules.exports as their variable names? – ludicrous Mar 05 '22 at 19:42
  • 1
    @ludicrous it is a considered as a better practice, but ... even Node.js creators added such an opportunity, to do it like that. So I guess it's not considered as something terrible. This did not work with older versions of Node.js and got fixed. From my personal point of view - the fastest solution is the best, and it is the fastest one. If you are going to be gitblamed for such a code? I have no idea. – Flash Thunder Mar 05 '22 at 19:49
  • 1
    @ludicrous but I can give you an example from real world... in Space Research Centre we got a strict programmer, who is doing everything as in the book. But it takes him like 100x more time because of that. People are actually mad at him, as everyone is expecting results. So I guess that's a matter of place, project, aim. – Flash Thunder Mar 05 '22 at 19:54
-1

Have you try this to change ":" by "=" like:

DEFAULT_PORT = 80;

module.exports = {
    badPort = (c_port, min=80, max=90) => {
                    return isNaN(c_port) || !between(c_port, min, max);
            },
    getPort = (c_port, min=80, max=90) => {
                    console.log(this.badPort(c_port));
                    return this.badPort(c_port) ? 80 : c_port;
            },
};  
Lucas Bodin
  • 311
  • 3
  • 19
-2

To reference badPort from the other function you can write it this way:

DEFAULT_PORT = 80;

const badPort = (c_port, min=80, max=90) => {
  return isNaN(c_port) || !between(c_port, min, max);
};

const getPort = (c_port, min=80, max=90) => {
  console.log(badPort(c_port));

  return badPort(c_port) ? 80 : c_port;
};

module.exports = {
  badPort,
  getPort
};     

And if you're curious on how to import it correctly, here is an example of a correct import from another js file in the same directory:

const port = require('./port.js');
console.log(port.badPort(1000));

And your default port can be an environment variable using process.env https://nodejs.org/dist/latest-v8.x/docs/api/process.html

benjaminknox
  • 257
  • 4
  • 11