0

I have a class like this:

class User {
    constructor(uname, pword) {
        if (!uname || !pword) 
            throw new Error("Insufficient parameters");

        if (typeof(uname) != "string" || typeof(pword) != "string") {
            this.username = "";
            this.password = "";
            logTo("error", "Bad parameter type.", thisFile);
            throw new Error("Bad parameter type.");
        } else {
            this.username = uname;
            this.password = pword;
            /* Validates the username and password format. */
            if (!this.validate()) {
                delete this; // Doesn't fullly delete, but removes reference- helps garbage collector to remove it.
                logTo("error", "Wrong username/password format when creating user.", thisFile);
                throw new Error("Wrong username/password format when creating user.");
            }
        }
    }
}

And I was wondering whether it's possible to only instantiate this class from a named function... The reason I ask is because I'm looking to make the permissions granular, aiming for higher security.

I tried to use arguments.caller.name, but I get an error message as so:

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them.
Lewis
  • 11
  • 1
  • possible duplicate ? https://stackoverflow.com/questions/21667149/how-to-define-private-constructors-in-javascript – Nisanth Reddy May 11 '21 at 13:55
  • 1
    "*instantiate this class only from a named function... The reason I ask is because I'm looking to make the permissions granular, aiming for higher security.*" - this doesn't make any sense whatsoever. What does this class do? How does it need to be secure? What kind of attacks, by what actors, are you trying to defend against? – Bergi May 11 '21 at 14:03
  • 1
    "*`delete this; // Doesn't fullly delete, but removes reference- helps garbage collector to remove it.`*" is just plain wrong – Bergi May 11 '21 at 14:04
  • Thank you, this article seems to do what I'm looking for. – Lewis May 11 '21 at 14:07
  • This class is used to create a new user. There's 3 types of user that I'm interested in, and I wanted to make it so that I can only instantiate the base 'user' class from within one of those three different types... The reason for this is because one user type can create others, but not the other way around. The permissions for this will be handled at DB level too- but I wanted to add another layer of abstraction over the top. Why did you quote the `delete this` section? Have I done something stupid? – Lewis May 11 '21 at 14:11
  • Does this answer your question? [How to define private constructors in javascript?](https://stackoverflow.com/questions/21667149/how-to-define-private-constructors-in-javascript) – Nisanth Reddy May 11 '21 at 14:14
  • @NisanthReddy, thank you yes- it does look like it answers my question. The example is a little terse, so I'll have to do some research. I was wording the question wrong, thank you for taking the time to help. – Lewis May 11 '21 at 14:18

0 Answers0