Avoiding conflicts between variables in Javascript

0

All modal windows in my system open via ajax, and these have their own JS codes with variables and functions. In order to avoid problems with the variables and functions of the screens that call these modal, I thought of adopting the following strategy in the modal JS:

HTML

<button id="btn">
Valor
</button>
<button id="btnSetar">
Setar
</button>

JS

var nome_da_tela = {
    init: function(){
        this.varUm = null;
        var thisLocal = this; 

        $("#btn").click(function(){
           alert(thisLocal.varUm);
        });

        $("#btnSetar").click(function(){
          thisLocal.SetarValor();
        });
    },
    SetarValor:function(){
        var thisLocal = this
        $.get('/echo/json/',{},function(data){
            thisLocal.varUm = data
        });
    }
}

$(document).ready(function(){
    nome_da_tela.init();
});

Follow link for testing. Have you ever had to do something like this? Do you have any better ideas? I did not like having to create a variable (thisLocal) just to reference the object outside its scope.

    
asked by anonymous 05.09.2016 / 16:37