简体   繁体   English

如何将 function 传递到 JavaScript 中的另一个 function

[英]how can I pass a function into another function in JavaScript

I have a function that check Username in a form,parent function i check if there are empty field, and now i wanna pass usernameValidat() in inside onSubmit() to check if it match with my username validation.我有一个 function 用于检查表单中的用户名,父 function 我检查是否有空字段,现在我想在 onSubmit() 中传递 usernameValidat() 以检查它是否与我的用户名验证匹配。 Can anyone give me the answer to solve this issue please任何人都可以给我解决这个问题的答案吗

onSubmit() {
            var text = "Please insert ";
            for (let i = 0; i < Constant.REGISTER_INFOS.length; i++) {
                if (Constant.REGISTER_INFOS[i].info == "") {
                    text = text + Constant.REGISTER_INFOS[i].title.toLowerCase() + ", ";
                }
            }
            alert(text);
            
        },
        usernameValidate() {
            if (Constant.REGISTER_INFOS[0].info.length < 4) {
                alert("Username is too short");
                return false;
            }
        },

Assuming that this is part of a Vue app, so something like:假设这是 Vue 应用程序的一部分,那么类似于:

const App = new Vue({
  methods: {
    onSubmit() {
      var text = "Please insert ";
      for (let i = 0; i < Constant.REGISTER_INFOS.length; i++) {
        if (Constant.REGISTER_INFOS[i].info == "") {
          text = text + Constant.REGISTER_INFOS[i].title.toLowerCase() + ", ";
        }
      }
      alert(text);
    },
    usernameValidate() {
      if (Constant.REGISTER_INFOS[0].info.length < 4) {
        alert("Username is too short");
        return false;
      }
    },
  }
});

Then you want to call another method using the this keyword, to access other methods on this Vue instance.然后,您想使用this关键字调用另一个方法,以访问此 Vue 实例上的其他方法。

So onSubmit() might look like:所以onSubmit()可能看起来像:

    onSubmit() {
      var text = "Please insert ";
      for (let i = 0; i < Constant.REGISTER_INFOS.length; i++) {
        if (Constant.REGISTER_INFOS[i].info == "") {
          text = text + Constant.REGISTER_INFOS[i].title.toLowerCase() + ", ";
        }
      }
      this.usernameValidate();
      alert(text);
    },

There are some big issues with this code though, and that won't actually do anything of note, as nothing is being done with the return value.虽然这段代码有一些大问题,但实际上并没有什么值得注意的,因为没有对返回值做任何事情。

A better way to handle this would be to:处理此问题的更好方法是:

  1. Return true or false from usernameValidate , depending on whether or not it's successful, and check it in if statement.usernameValidate返回 true 或 false,取决于它是否成功,并在if语句中检查它。
  2. Change the signature of the usernameValidate method to take an argument .更改usernameValidate方法的签名以获取argument This means that we can re-use this functionality with any input, making it a lot more flexible.这意味着我们可以对任何输入重复使用此功能,使其更加灵活。

With these two changes, we end up with the following:通过这两个更改,我们最终得到以下结果:

const App = new Vue({
  methods: {
    onSubmit() {
      var text = "Please insert ";
      for (let i = 0; i < Constant.REGISTER_INFOS.length; i++) {
        if (Constant.REGISTER_INFOS[i].info == "") {
          text = text + Constant.REGISTER_INFOS[i].title.toLowerCase() + ", ";
        }
      }
      if (this.usernameValidate(Constant.REGISTER_INFOS[0].info) {
        alert(text);
        return true;
      }
      alert("Registration Failed!");
      return false;
    },
    usernameValidate(username) {
      if (username.length < 4) {
        alert("Username is too short");
        return false;
      }
      return true;
    },
  }
});

Hopefully that clears a few things up, and can give you a little more insight in to programming in general.希望这可以解决一些问题,并且可以让您对总体编程有更多的了解。

Having said all of that, I would highly recommend learning JavaScript from scratch, before getting stuck in to frameworks (which VueJS is) and things like that.说了这么多,我强烈建议从头开始学习 JavaScript,然后再陷入框架(VueJS 就是这样)之类的东西。 Understanding the core principles of programming, conditional logic, etc., will make you much more competent in the long run, and just grabbing at code snippets from StackOverflow will just lead to you pulling your hair out, and being stuck on a problem for days at a time.从长远来看,了解编程、条件逻辑等的核心原则会让你更有能力,而仅仅从 StackOverflow 中抓取代码片段只会让你大发雷霆,并被困在一个问题上好几天一次。

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

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