Home>

I would like to support flg processing using functions.
I want to do the process of giving a number to the argument, taking the variable name from the array, putting it in the variable, and setting it to true if the flg is false

At present, flg is recognized as a sentence.

$a_flg
$b_flg ...
It manages flg with the variable of.

var $a_flg = false;
var $b_flg = false;
var $c_flg = false;
function flgFunction (number) {
    // determine each flg
    var arr = ['$a_flg', '$b_flg', '$c_flg']
    var this_flg = arr [number];
    // Set flg to true
    if (this_flg == false) {
        this_flg = true;
    }
    return this_flg;
}
flgFunction (0);// $a_flg


If argument 1 is passed
$a_flg is the return value.

Ideally

true is the return value.

  • Answer # 1

    flags = {"$a": true};
    flags ["$a"] = false;


    If you do it,

    function flgFunction (number) {
        // determine each flg
        var arr = ['$a_flg', '$b_flg', '$c_flg']
        var this_flg = flags [arr [number]];
        // Set flg to true
        if (this_flg == false) {
            this_flg = true;
        }
        return this_flg;
    }


    So, I think that it can be done, and if you examine it with dynamic variables, I think that various ways will come out, but making it is the worst.
    I don't know what I want to do, but I think it's better to stop now that the function goes directly to the outer (global) $a_flg.

  • Answer # 2

    If you want to declare and use a function dynamically, you need a little trick.

    Calling JS functions dynamically

    Execute a function with a string

    Mechanism to call function dynamically by specifying function name (without using eval)

    StackOverflow: Dynamic function call with javascript

  • Answer # 3

    First of all, $a_flg will not be returned if it is an example, it is $b_flg

    Checking with if (this_flg == false) does not return false because it is not false
    What did you think $b_flg was false based on?

    Quotation even if $b_flg is the letter "false" even after interpretation Since it is enclosed, it is judged by the character string. If this_flg == false, it will not be hit.

    In addition, operators that check for false should never use "=="
    Please use "===". (It may hit 0 or null other than false)

Related articles