简体   繁体   English

变量值未跨函数更新

[英]Variable values not being updated across functions

The variable Message and boolean value count from function validator won't get updated when it runs inputFocus function. 当运行inputFocus函数时,来自函数validator的变量Message和布尔值count不会被更新。 So, validator always returns true. 因此, validator始终返回true。

(function(){
    var form = document.forms[0];
    form.addEventListener('submit', function(evt){
        if(!validator(form)){
            evt.preventDefault();
        }
    }, false);      
})();

main.js main.js

function validator(form){
    var message = "The input below is invalid";
    var count = true;
    if(!phoneValidation(form.phone.value)){
        inputFocus(form['phone'], "focus", message, form.phone.name, count);
    } 

    if(!count){
        alert(message);
        return count;
    } else {
        return true;
    }
}

function inputFocus(element, newClassName, message, id, count){
    element.className += " " + newClassName;
    message += ", " + id;   
    if (count){
        count = false;
    }
}
function inputFocus(element, newClassName, message, id, count){
    element.className += " " + newClassName;
    message += ", " + id;   
    if (count){
        count = false;
    }
}

So when you pass the parameters (including message and count ) to inputFocus , given the fact that they are scalars (a boolean and a string), then whatever inputFocus does with the variables local to it "stays" within inputFocus . 因此,当您将参数(包括messagecount )传递给inputFocus ,鉴于它们是标量 (一个布尔值和一个字符串),那么无论inputFocus局部变量执行的操作,都会“停留”在inputFocus This is because it's passed the values of the scalars, not the variables per se. 这是因为它传递了标量的 ,而不是变量本身。

In short: 简而言之:

inputFocus doesn't change whatever variables whose values happened to be passed to it. inputFocus不会更改碰巧将其传递给它的任何变量。

One potential way to solve this is to have inputFocus return the correct values of count and message , then validator can do as it wishes with those values. 解决此问题的一种可能方法是让inputFocus返回countmessage的正确值,然后validator可以根据需要使用这些值进行操作。

Hope this gets you on the right track. 希望这能使您走上正确的道路。

Move the variables Message and count outside all the functions to make it like global. 将变量Message移到所有函数之外并计数,以使其类似于global。 This will let any javascript function in the page to update them 这将允许页面中的所有javascript函数更新它们

var message;
var count;
function validator(form){
    message = "The input below is invalid";
    count = true;
    if(!phoneValidation(form.phone.value)){
        inputFocus(form['phone'], "focus", message, form.phone.name, count);
    } 

    if(!count){
        alert(message);
        return count;
    } else{
        return true;
    }
}

function inputFocus(element, newClassName, message, id, count){
    element.className += " " + newClassName;
    message += ", " + id;   
    if (count){
        count = false;
    }
}

At the same time its not a good practice to use global variables if you can merge the functions that uses these variables into one. 同时,如果您可以将使用这些变量的函数合并为一个,则不建议使用全局变量。

That's because when you set the value of message and count within the inputFocus function, the value of those variables are only available within the scope of that function. 这是因为当您在inputFocus函数中设置message的值并count ,这些变量的值仅在该函数的范围内可用。

Here's an evening's worth of reading. 这是一个值得一读的夜晚。 Absolutely worth it if you want to write javascript. 如果您想编写javascript,绝对值得。

  1. What is the scope of variables in JavaScript? JavaScript中变量的范围是什么?
  2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions_and_function_scope
  3. http://coding.smashingmagazine.com/2009/08/01/what-you-need-to-know-about-javascript-scope/ http://coding.smashingmagazine.com/2009/08/01/what-you-need-to-know-about-javascript-scope/
  4. http://blog.kevinchisholm.com/javascript/scope/ http://blog.kevinchisholm.com/javascript/scope/

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

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