How to replace Eval() with another expression

Asked

Viewed 460 times

4

Reading some comments and especially the comments of Mr Tobymosque, I try today to avoid the use of Eval(). Well, now comes the following. In simple expressions, how do I replace it? See below a variable statement that I took here in the system that I work on and see if my approach is correct? Due to a problem we had here today, I’m not being able to test and in jsfiddle I can’t, because the page has a lot called Asp and is extremely large, because there is dependency on other pages.

function prorrogaVencimento(pLinha) {
        var intI = pLinha;
        var v_num_seq_cobranca = eval('form01.num_seq_cobranca_' + intI + '.value');
        var v_nom_tipo_ciclo = eval('form01.nom_tipo_ciclo_' + intI + '.value');

        var v_txt_num_linha_digitavel = eval('form01.num_linha_digitavel_' + intI + '.value');
        var v_num_seq_fatura_ts = eval('form01.num_seq_fatura_ts_' + intI + '.value');
        var v_ind_tipo_cobranca = eval('form01.ind_tipo_cobranca_' + intI + '.value');
        var v_mes_ano_ref = eval('form01.mes_ano_ref_' + intI + '.value');
        var v_ind_situacao = eval('form01.ind_situacao_' + intI + '.value');
        if ((v_ind_tipo_cobranca == '5' || v_ind_tipo_cobranca == '6') & v_ind_situacao == 'Vencidas' {
            alert('Ação não permitida para o tipo da cobrança.');
            return;
        }

I did so, but I think it’s bad:

var v_num_seq_cobranca = eval('form01.num_seq_cobranca_' + intI + '.value');

for that reason:

var v_num_seq_cobranca = form01.num_seq_cobranca_[intI].value;

There’s that other approach too:

var v_num_seq_cobranca = window['form01.num_seq_cobranca_' + intI].value;

2 answers

6


In fact this change will not produce the expected result. However, in Javascript all property can be accessed through brackets. So that:

foo.bar

It’s the same as that:

foo["bar"]

And by the way, being window the global object, the same access can be done also this way (but only if foo is a global variable):

window["foo"]["bar"]

Thus, a way to access a property whose name is only known at runtime is to "mount" the string and using the brackets to access the property with the corresponding name:

var v_num_seq_cobranca = form01['num_seq_cobranca_' + intI].value;

Note that as much form01 how much value are static, you can access them normally (by name, and by dot), without needing to create an explicit string for this.

Addendum

Your first attempt:

var v_num_seq_cobranca = form01.num_seq_cobranca_[intI].value;

Does not produce the expected result because it is accessing a property called num_seq_cobranca_ and within it trying to access one whose name is the value of intI:

var intI = 10;
var v_num_seq_cobranca = form01["num_seq_cobranca_"][intI]["value"]

Already his second attempt:

var v_num_seq_cobranca = window['form01.num_seq_cobranca_' + intI].value;

You are trying to access a call variable form01.num_seq_cobranca_10 (yes, global properties and variables can contain special characters! But only if declared/accessed in this way...), and not two properties within each other.

var v_num_seq_cobranca = window['form01.num_seq_cobranca_10']['value'];
  • Thanks, I opted for your first opening which is the same as the bfavaretto.

  • In reality this approach is the "only" correct (in the sense of "minimal"). The rest of the answer is just exemplifying the concepts, and showing what would be the result of your other attempts.

4

I would do so:

var v_num_seq_cobranca = form01['num_seq_cobranca_' + intI].value;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.