English 中文(简体)
回返职能作为关闭的参数
原标题:Returning function as parameter with closures
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-zd]{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)
  }
}

我试图利用关闭办法建立认证。 我正努力使我的守则尽可能高效。

我想谈谈每个投入部分(不逐一选择),并运用每个部分的活动听众。 输入Check功能将视每项投入的名称特性而恢复有效器功能,而创建生计功能则采用投入Check的回报价值,该表是特定类型的有效器,然后用于测试目的,即独角。 真实或虚假。

迄今为止,唯一从事输入Check职能的分支是第一个与姓名属性电子邮件有关的分支。 如果我把价值与其他投入要素混为一谈并提交表格,则分行会打工。

谁能告诉我我我我我我我我,我会错了吗?

我对结束发言说几句新话,因此我理解,这个问题对你们大多数人来说似乎相对简单。

问题回答

I can observe two things:

首先,正如“VLAZ”指出的,有两条 con。 投入记录 检查实际上没有执行,因为这些检查是在返回后进行的。

Second, createListener and validation are not quite right. createListener returns a function with one argument. validation forEach doesn t log anything because createListener returns a function, no function execution here.

还有另一个问题,即<代码>e>> 。 你似乎认为这是一个事件,但根据你的执行情况,只有一个事件,即提交活动。 因此,我建议对这两项职能作一点修改:

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/





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签