How to replace eval () with another expression

4

Reading some comments and especially comments from colleague TobyMosque, I try today to avoid using eval (). Well, now comes the next. In simple expressions, how do I replace it? See below a variable declaration that I got here on the system I work on and see if my approach is correct? Due to a problem we had here today, I'm not able to test and in jsfiddle it does not, because the page has lots of asp call and is extremely large, as there is dependence 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 it, but I think it's bad:

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

for this:

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

You have this other approach too:

var v_num_seq_cobranca = window['form01.num_seq_cobranca_' + intI].value;
    
asked by anonymous 25.08.2015 / 15:43

2 answers

6

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

foo.bar

It's the same as this:

foo["bar"]

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

window["foo"]["bar"]

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

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

Note that because both% and% of% are static, you can normally access them (by name and by point) without having to create an explicit string for it.

Addendum

Your first attempt:

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

It does not produce the expected result because it is accessing a property called form01 and inside it trying to access one whose name is the value of value :

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

Your second attempt:

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

You are trying to access a variable called num_seq_cobranca_ (yes, properties and global variables can contain special characters! But only if declared / accessed this way ...), not two properties one inside the other.

var v_num_seq_cobranca = window['form01.num_seq_cobranca_10']['value'];
    
25.08.2015 / 15:54
4

I would do so:

var v_num_seq_cobranca = form01['num_seq_cobranca_' + intI].value;
    
25.08.2015 / 15:50