简体   繁体   English

在尝试验证功能中无法访问javascript父函数 - 可能需要关闭

[英]Can't access javascript parent function in attempted validation function - maybe closure needed

I'm trying to write a simple function which would validate an input but I'm having trouble accessing a parent object function. 我正在尝试编写一个简单的函数来验证输入,但是我无法访问父对象函数。

I think the problem is that I need a closure but as I'm quite new to js and I'm having difficulty getting my head around closures I thought maybe seeing it in action in my code might help, if it is indeed the problem. 认为问题是我需要一个闭包,但是因为我对js很新,而且我很难理解闭包,我想也许在我的代码中看到它可能会有所帮助,如果确实是问题的话。

function validate(value, validator){

  if(validator == 'login_cred'){
    testName(value);
  }

  var test = {
    minLength: function (val, length) {
      return val.length >= length;
    }
  }

  function testName(value){
    if(!test.minLength(value, 5)){
      console.log('more chars please...');      
    }      
  }

}

//call
validate("str", 'login_cred');

When I call the function I get test is undefined error. 当我调用该函数时,我得到的测试是未定义的错误。

Is this a case of needing a closure?.. if so how would a closure work best in the above code? 这是一个需要闭包的情况吗?..如果是这样,在上面的代码中闭包效果如何?

No, the variable is in scope and you don't need an extra closure. 不,变量在范围内,您不需要额外的闭包。 Only you assign to the var test after you call testName which uses it. 只有调用使用它的testName 后才分配给var test Move the test object (and the testName declaration) to the top: test对象(和testName声明)移动到顶部:

function validate(value, validator) {
    function testName(value) {
        if (!test.minLength(value, 5)) {
            console.log('more chars please...');
        }
    }
    var test = {
        minLength: function (val, length) {
            return val.length >= length;
        }
    }
    if (validator == 'login_cred') {
        testName(value);
    }
}
//call
validate("str", 'login_cred');

Of course, unless you have some more code in that function, you just should reduce it to 当然,除非你在该函数中有更多代码,否则你应该将它减少到

function validate(value, validator) {
    if (validator == 'login_cred' && value.length < 5)
        console.log('more chars please...');
}

:-) :-)

The function testName gets hoisted. 函数testName被提升。 Because of this you code actually looks like this. 因此,您的代码实际上看起来像这样。

function validate(value, validator){

  function testName(value){
    if(!test.minLength(value, 5)){
      console.log('more chars please...');      
    }      
  }

  if(validator == 'login_cred'){
    testName(value);
  }

  var test = {
    minLength: function (val, length) {
      return val.length >= length;
    }
  }

}

//call
validate("str", 'login_cred');

as you can see test has not been declared yet when it is called, and simple placing testName at the bottom does not change this. 正如您所看到的那样,在调用test时尚未声明test,并且简单地将testName放在底部不会改变它。 Simply change your code to this and it will work. 只需将您的代码更改为此代码即可。

function validate(value, validator){

 var test = {
    minLength: function (val, length) {
      return val.length >= length;
    }
  }

  if(validator == 'login_cred'){
    testName(value);
  }

  function testName(value){
    if(!test.minLength(value, 5)){
      console.log('more chars please...');      
    }      
  }

}

//call
validate("str", 'login_cred');

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

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