I am trying to create a helper function that in part takes in a global variable for reassignment. Trying to avoid using eval to make this work.
let oneQuestionCount,
twoQuestionCount,
oneQuestionsChecked,
twoQuestionsChecked;
function hideFadeIn(divToHide, divToShow, countItem = null, checkedItem = null) {
$("div#item-" + divToHide).hide();
$("div#item-" + divToShow).fadeIn();
if (countItem) {
countItem = $("div#item-" + divToShow + " :input").length; // want the global var to be changed not the function scope var
}
if (checkedItem) {
checkedItem = $("div#item-" + divToHide + " :checked").length; // want the global var to be changed not the function scope var
}
hideFadeIn(
"one",
"two",
twoQuestionCount, // how to pass in and change globally?
oneQuestionsChecked // how to pass in and change globally?
);
console.log(twoQuestionCount, oneQuestionsChecked); // should be reassinged value by the function.
There will be multiple function calls and other global variables that need to be assigned - hence the helper function. ex: hideFadeIn("one","two",twoQuestionCount, oneQuestionsChecked); then hideFadeIn("two","three",threeQuestionCount, twoQuestionsChecked); then hideFadeIn("three","four",threeQuestionCount, fourQuestionsChecked); etc...
You can't do that. When you pass twoQuestionCount
into hideFadeIn
, its value is passed, not the variable.
If you like, you could put those in an object, and then pass in the object:
let whatever = {
oneQuestionCount: 0,
twoQuestionCount: 0,
oneQuestionsChecked: 0,
twoQuestionsChecked: 0
};
then
hideFadeIn("one", "two", whatever);
hideFadeIn
can change the properties on the object it receives.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.