简体   繁体   English

返回 function 作为带闭包的参数

[英]Returning function as parameter with closures

Here is a link to my JS fiddle: https://jsfiddle.net/apasric4/v1qkmgyu/1/这是我的 JS 小提琴的链接: https://jsfiddle.net/apasric4/v1qkmgyu/1/

function inputCheck(input) {
  if (input.name==="email") {
    console.log("email")
    return isValidEmail
  } else if (input.name==="password") {
    return isValidPassword
    console.log("pass")
  } else if (input.name==="userName") {
    return isValidUserName
    console.log("user")
  }
}


function isValidEmail (email) {
  return /^[^@]+[@][^@.]+\.[a-z]+$/.test(email)
}

function isValidPassword(pass) {
  return /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/.test(pass)
}

function isValidUserName(user) {
  return /^[a-zA-Z0-9]+([_ -]?[a-zA-Z0-9])*$/.test(user)
}


function validation(e) {
  e.preventDefault()
  inputs.forEach(input=> createListener(inputCheck(input)))
}



function createListener(validator) {
  return (e)=> {
    const inputValue=e.target.value;
    const valid=validator(inputValue)
    console.log(valid)
  }
}

I'm trying to create form validation using closures.我正在尝试使用闭包创建表单验证。 I am trying to make my code as efficient as possible.我正在努力使我的代码尽可能高效。

I want to loop over each input element (without selecting each individually), and apply an event listener to each one.我想遍历每个输入元素(而不是单独选择每个元素),并对每个元素应用一个事件侦听器。 The inputCheck function would return a validator function depending on the name attribute of each input, and the createListener function takes the value returned by inputCheck, which would be a specific type of validator, and then for testing purposes, console.log true or false. The inputCheck function would return a validator function depending on the name attribute of each input, and the createListener function takes the value returned by inputCheck, which would be a specific type of validator, and then for testing purposes, console.log true or false.

So far, the only if branch that works in the inputCheck function is the first one associated with name attribute email.到目前为止,在 inputCheck function 中工作的唯一 if 分支是第一个与名称属性 email 关联的分支。 The other if branches won't work if I type values into other input elements and submit the form.如果我在其他输入元素中键入值并提交表单,则另一个 if 分支将不起作用。

Can anyone tell me where I'm going wrong and how to improve my code?谁能告诉我哪里出了问题以及如何改进我的代码?

I'm new to closures so I understand that this issue might seem relatively simple to most of you.我是闭包的新手,所以我知道这个问题对你们大多数人来说似乎相对简单。

I can observe two things:我可以观察到两件事:

First, just like @VLAZ pointed out, two console.log in inputCheck are actually not executed since they are placed after return.首先,就像@VLAZ 指出的那样, inputCheck 中的两个console.log 实际上没有执行,因为它们是在返回之后放置的。

Second, createListener and validation are not quite right.其次, createListenervalidation不太对。 createListener returns a function with one argument. createListener返回带有一个参数的 function。 validation forEach doesn't log anything because createListener returns a function, no function execution here. validation forEach 不会记录任何内容,因为createListener返回 function,此处没有 function 执行。

There is another problem with the argument e of createListener . createListener的参数e还有另一个问题。 It seems like you treat it as an event, but based on your implementation, there is only one event, that is form submit event.似乎您将其视为一个事件,但根据您的实现,只有一个事件,即表单提交事件。 So, I'd suggest to modify these two functions a little bit:所以,我建议稍微修改一下这两个函数:

function validation(e) {
  e.preventDefault()
  inputs.forEach(input=> createListener(inputCheck(input))(input))
}



function createListener(validator) {
  return (e)=> {
    const inputValue=e.value;
    const valid=validator(inputValue)
    console.log(valid)
  }
}

Then, the console prints out true or false based on the input value of each input field.然后,控制台根据每个输入字段的输入值打印出真假。 Please check whether the output is your intension or not https://jsfiddle.net/jqgbefhw/请检查 output 是否是您的意图https://jsfiddle.net/jqgbefhw/

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

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