简体   繁体   English

有没有更好的方法在javascript中编写return语句

[英]is there a better way to write return statement in javascript

I'm creating a sign up page using prompt(), i want to return from the page when a user cancels.我正在使用 prompt() 创建一个注册页面,我想在用户取消时从该页面返回。 i tried using return statement to achieve this but it gave an error _ illegal return statement .我尝试使用 return 语句来实现这一点,但它给出了一个错误 _非法 return 语句 I wrote the same code without a constructor or the idea of OOP and I really want to get same result using constructor or the idea of OOP if possible here is my code我在没有构造函数或 OOP 的想法的情况下编写了相同的代码,如果可能的话,我真的想使用构造函数或 OOP 的想法获得相同的结果这是我的代码

let getuserName = prompt("enter your user Name")

while (validateUserName(getuserName) == false) {
    getuserName = prompt("invalide user, user name most be less than 10")

}

if (getuserName == null) {
    return
}* //this is showing illegal return*

let getpassword = prompt("enetr your password")

while (validatePassword(getpassword) == false) {
    getpassword = prompt("password most be 10 0r more")

}

//confirm password

let getconfirmPassword = prompt("please confirm your password")

while (getconfirmPassword !== getpassword) {
    getconfirmPassword = prompt("invalid password, please enter a valid password")

}


class user {

    constructor(userName, firstName, lastName, email, accountValidated, password, confirmPassword) {
        this.userName = userName || "Anonymous";
        this.firstName = firstName || "Anonymous";
        this.lastName = lastName || "N/A";
        this.email = email || " "
        this.accountValidated = accountValidated || null;
        this.password = password || null;
        this.confirmPassword = confirmPassword || null;
    }
    getuserName() {
        return this.userName;
    }

    getfullName() {
        return `${this.firstName} ${this.lastName}`;
    }

    getemail() {
        return this.email;
    }

    getvalidation() {
        return this.accountValidated;
    }

    getpassword() {
        return this.password;
    }

    getconfirmPassword() {
        return this.confirmPassword
    }

}

const user1 = new user(`${getuserName}`, "josephine", "nnalue", "josephinennalue@gmail.com", true, "josephine123")
const user2 = new user("Amara123", "Amarachi", "Simon", "amarachisimon@gmail.com", true, "amarachi123123")
const user3 = new user("chi123", "Chika", "Okoye", "chikaokoye@gmail.com", false, "chika123")
const user4 = new user("ble123", "Blessing", "Jimmy", "blessingjimmy@gmail.com", true, "blessing123")
const user5 = new user("seunbaby", "Seun", "Kareem", "seunkareem@gmail.com", false, "seun123")




console.log(user1.getuserName())

//validating user name

function validateUserName() {
    if (getuserName == null) {
        return true
    }

    if (getuserName.length > 10) {
        return false
    } else {
        return true
    }
}


console.log(validateUserName())


//validating password

function validatePassword() {
    if (getpassword.length < 10) {
        return false
    } else {
        return true
    }
}

console.log(validatePassword())

I've made some adjustments to your code to make it work and also simplified some of it.我对您的代码进行了一些调整以使其正常工作并简化了其中的一些。 I've tried to reuse your code and logic as much as possible.我试图尽可能地重用你的代码和逻辑。

 function showPrompt() { function validateUserName(userName) { return userName.length < 10; } function validatePassword(password) { return password.length > 10; } let getuserName = prompt("enter your user Name") while (!validateUserName(getuserName)) { getuserName = prompt("invalide user, user name most be less than 10") } let getpassword = prompt("enter your password") while (!validatePassword(getpassword)) { getpassword = prompt("password most be 10 or more") } let getconfirmPassword = prompt("please confirm your password") while (getconfirmPassword !== getpassword) { getconfirmPassword = prompt("invalid password, please enter a valid password") } return { username: getuserName, password: getpassword } } const inputs = showPrompt(); class user { constructor(userName, firstName, lastName, email, accountValidated, password, confirmPassword) { this.userName = userName || "Anonymous"; this.firstName = firstName || "Anonymous"; this.lastName = lastName || "N/A"; this.email = email || " " this.accountValidated = accountValidated || null; this.password = password || null; this.confirmPassword = confirmPassword || null; } getuserName() { return this.userName; } getfullName() { return `${this.firstName} ${this.lastName}`; } getemail() { return this.email; } getvalidation() { return this.accountValidated; } getpassword() { return this.password; } getconfirmPassword() { return this.confirmPassword } } const user1 = new user(`${inputs.username}`, "josephine", "nnalue", "josephinennalue@gmail.com", true, "josephine123") const user2 = new user("Amara123", "Amarachi", "Simon", "amarachisimon@gmail.com", true, "amarachi123123") const user3 = new user("chi123", "Chika", "Okoye", "chikaokoye@gmail.com", false, "chika123") const user4 = new user("ble123", "Blessing", "Jimmy", "blessingjimmy@gmail.com", true, "blessing123") const user5 = new user("seunbaby", "Seun", "Kareem", "seunkareem@gmail.com", false, "seun123") console.log(user1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM