javascript - 为什么这个函数不停止它的父函数?

标签 javascript function

我创建了一个函数,如果给定条件,该函数将停止其父函数:

handleUserSubmit () {
  this.userForm.options.hasFormSubmitted = true
  if (!this.userForm.options.isFormValid) return
},

handleUpdateUser () {
  const fields = this.userForm.schema
  this.userInput.buildId = this.user.objectId
  this.handleUserSubmit()
  // rest of code
}

但是,无论条件是什么,其余代码都会运行。我做错了什么?

最佳答案

将条件返回移至您需要返回的函数:

handleUserSubmit () {
  this.userForm.options.hasFormSubmitted = true
  return !this.userForm.options.isFormValid;
},

handleUpdateUser () {
  const fields = this.userForm.schema
  this.userInput.buildId = this.user.objectId
  if(this.handleUserSubmit()) return;
  // rest of code
}

关于javascript - 为什么这个函数不停止它的父函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41031239/

相关文章:

javascript - 迭代一个数字中的数字,只要它是相同的数字

javascript - 当绑定(bind)中的值更改时, knockout 计算函数不会更新

java:观察一个函数直到给定时间

javascript - 如何找到字符串中最长的匹配字母序列?

c++ - 通过引用传递的对象未按预期工作

JavaScript if 语句只评估第一个条件

javascript - 使用 jquery 加载 div 后执行一些操作

javascript - Nodejs API 服务器中错误处理的正确方法

objective-c - 在 objective-c 中使用参数作为变量名

function - Common Lisp 函数没有返回值