T
T
tlopster2021-07-28 14:42:44
Node.js
tlopster, 2021-07-28 14:42:44

How to make a setter method in a module?

Good afternoon!

main.js file

const check = require("./test.js")
check("dh39dk")


test.js file
function checkToken(token) {
if(!arr[token]) return false
return true
}
module.exports = checkToken


How to make the main.js code look like this:

const check = require("./test.js")
check.setToken("dh39dk")
check()


Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rag'n' Code Man, 2021-07-29
@tlopster

class TokenValidator {
    constructor(token) {
        this.token = token
    }

    check() {
       if (!arr[this.token]) return false
       return true
    }
}

const validator = new TokenValidator("dhejeuus")
console.log(validator.check())

Or (if you really want the setToken function)
class TokenValidator {
    setToken(token) {
        this.token = token
    }

    check() {
       if (!arr[this.token]) return false
       return true
    }
}

Well, and another option with a function prototype, but here already.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question