JavaScript非法return语句报错的OOP写法解决方案
错误原因
illegal return statement报错的核心原因是:JavaScript中return关键字只能在函数、类方法的内部使用,你当前写的return语句处于全局顶级作用域,引擎无法识别该返回操作的目标上下文,直接抛出语法错误。
你之前非OOP写法能正常运行,本质是当时整段逻辑被包裹在某个函数作用域内,return有合法的上下文,和是否用OOP写法没有直接关系。
另外你的原有代码还存在几个隐性问题:
- 调用
validateUserName、validatePassword时传入了参数,但两个函数都没有定义形参,完全依赖全局变量传值,耦合度极高 - 对用户点击取消(prompt返回
null)的判断位置靠后,如果用户第一次弹出输入框就点取消,后续逻辑访问null.length会直接抛出类型错误 - 类名不符合JavaScript通用命名规范,普通类名需要使用大驼峰格式
OOP写法修复方案
把整个注册流程的输入采集、校验、用户实例生成都封装到独立的注册流程类中,所有终止逻辑都在类方法内部执行,既符合OOP封装的思想,也能解决return非法的问题,修复后完整代码如下:
// 用户实体类 遵循大驼峰命名规范 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; } getValidationStatus() { return this.accountValidated; } getPassword() { return this.password; } getConfirmPassword() { return this.confirmPassword; } } // 注册流程类 封装所有输入、校验、流程控制逻辑 class RegistrationProcess { constructor() { this.userName = null; this.password = null; this.confirmPassword = null; } // 校验用户名 接收待校验值作为参数 不依赖全局变量 #validateUserName(name) { // 用户点取消直接判定通过 走后续终止逻辑 if (name === null) return true; return name.length <= 10; } // 校验密码 #validatePassword(pwd) { if (pwd === null) return true; return pwd.length >= 10; } // 启动注册流程 start() { // 采集用户名 this.userName = prompt("enter your user Name"); while (!this.#validateUserName(this.userName)) { this.userName = prompt("invalid user, user name must be less than 10 characters"); } // 用户点取消 直接在方法内return终止 符合语法规则 if (this.userName === null) { console.log("注册已取消"); return; } // 采集密码 this.password = prompt("enter your password"); while (!this.#validatePassword(this.password)) { this.password = prompt("invalid password, password must be 10 characters or more"); } if (this.password === null) { console.log("注册已取消"); return; } // 采集确认密码 this.confirmPassword = prompt("please confirm your password"); while (this.confirmPassword !== this.password) { if (this.confirmPassword === null) { console.log("注册已取消"); return; } this.confirmPassword = prompt("password mismatch, please enter the same password again"); } // 所有输入完成 生成用户实例 const newUser = new User( this.userName, "josephine", "nnalue", "josephinennalue@gmail.com", true, this.password, this.confirmPassword ); console.log("注册成功,用户名:", newUser.getUserName()); return newUser; } } // 启动注册 const register = new RegistrationProcess(); register.start(); // 原有预置测试用户可保留 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")
核心改动说明
- 拆分实体类和流程类:
User类只负责存储用户信息、提供访问属性的方法,RegistrationProcess类专门负责注册流程控制,符合单一职责的OOP设计原则 - 所有流程控制逻辑都在
start()方法内部,需要终止注册时直接return,完全符合JS语法要求,不会再抛出非法return的错误 - 每一步输入后第一时间判断用户是否点击取消,不会出现访问
null属性的类型错误 - 校验方法改为接收参数做判断,不再依赖全局变量,后续调整校验规则不需要改动全局逻辑
内容的提问来源于stack exchange,提问作者Amarachi
相关产品推荐
相关产品推荐

