A
A
Arthur Belan2020-06-20 01:10:22
JavaScript
Arthur Belan, 2020-06-20 01:10:22

JS how to refer to the method class, in this example?

How to access a class method? the User class and two extends of the same class, there is also an if function in it - there we assign the object to a variable. At the end, we take the isAdmin method from CommonUser and write it to document.cookie. The bottom line is that the browser does not find the desired object, or rather, I can not understand.
There is a problem with the scope, but if you set the cookie entry directly in the if near beUser and wrap it all in return () => {...} it won't work anyway, tell me how to do it right.
At the very bottom, an appeal to the class!

let isAdmin = false;

class User {
    constructor(userName, phone, email, psw, country, postCode, userText) {
        this.userName = userName;
        this.phone = phone;
        this.email = email;
        this.psw = psw;
        this.country = country;
        this.postCode = postCode;
        this.userText = userText;
    }
}

class CommonUser extends User {
    constructor(
        userName,
        phone,
        email,
        psw,
        country,
        postCode,
        userText,
        isAdmin
    ) {
        super(userName, phone, email, psw, country, postCode, userText);
        this.isAdmin = isAdmin;
    }
}

class AdminUser extends User {
    constructor(
        userName,
        phone,
        email,
        psw,
        country,
        postCode,
        userText,
        isAdmin
    ) {
        super(userName, phone, email, psw, country, postCode, userText);
        this.isAdmin = isAdmin;
    }
}

function registrationUser() {
    if (isAdmin) {
        let beAdmin = new AdminUser(
            userName.value,
            phone.value,
            email.value,
            psw.value,
            country.value,
            postCode.value,
            userText.value,
            isAdmin
        );
        messageUserOrAdmin.innerHTML = `${beAdmin.userName} you is admin`;
        console.log(`${beAdmin.userName} you is admin`);
        console.log(beAdmin);
        console.log(document.cookie);
    } else {
       let beUser = new CommonUser(
            userName.value,
            phone.value,
            email.value,
            psw.value,
            country.value,
            postCode.value,
            userText.value,
            isAdmin
        );
        messageUserOrAdmin.innerHTML = `${beUser.userName} you don't admin`;
        console.log(`${beUser.userName} you don't admin`);
        console.log(beUser);
    }
}

let adminAccess = beUser.isAdmin;
document.cookie = `${adminAccess} max-age=3000;`;
console.log(document.cookie);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SevSergei, 2020-06-20
@Belartale

your architecture is crooked:
1) that AdminUser, that CommonUser perform the same thing. You can get rid of CommonUser, since in fact it is User
2) You must pass the created user object to the register method, and not create it inside the method. The User class can have a getRole method, which will be used to check inside the method (if necessary)
3) It's easier to use a ready-made module for working with cookies, for example, js-cookie - there will be more readable code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question