Try / catch in JS

5

I have an array called agenciasUri , which formats the data before inserting it into it. After that, I set the JSON jsonObj to send a request. What I find strange is that it's working properly.

Why can I access agenciasUri ? How does visibility of a variable within a JavaScript loop (life cycle) work?

$scope.salvar = function () {
        var agenciasSeparadas = $scope.opcoes + '';
        agenciasSeparadas = agenciasSeparadas.split(',');
        try {
            var agenciasUri = [];
            for (var i = 0; i < $scope.listaAgencias.length; i++) {
                var json = $scope.listaAgencias[i];
                for (var k = 0; k < agenciasSeparadas.length; k++) {
                    if (json.nome == agenciasSeparadas[k]) {
                        agenciasUri.push(json._links.self.href);
                    }
                }
            }
        } catch (err) {
            alert(err);
        }

        var jsonObj = {
            nome: $scope.nome,
            tipo: $scope.tipo,
            agencias: agenciasUri
        };



        alert(JSON.stringify(jsonObj));
        veiculosAPI.postVeiculo(jsonObj)
            .success(function (data, status, headers, config) {
                $scope.nome = null;
                $scope.tipo = null;
            }).error(function (data, status, headers) {
                alert("Erro ao salvar dados do veiculo!");
                alert(data + "\t" + JSON.stringify(data));
            });

    };
    
asked by anonymous 23.07.2015 / 13:55

1 answer

7

The point is that JavaScript has the scope of variables declared with var local to the function and not to the execution block, as it happens in other languages. This phenomenon is known as hoisting .

So no matter where you declare the variable, it will exist throughout the function. But not out of it.

Only variables declared outside the function have global or at least regional scope. By regional understand that a variable can be accessible within the function where it was declared and within functions contained in this function.

With EcmaScript 6 you can use let that allows the use of block scope, as found in other languages. More details on the difference between them can be found on this question .

The idea of the scope of var is not bad, the problem is that it has been poorly implemented. If you declare the same variable within a function twice, in different blocks, it is accepted without errors. This should not happen. But by the philosophy of language the second statement usually functions as if it were an assignment. This is confusing. Another problem is that the variable exists even though it has been declared after its use.

In this way, the "modern" style of programming is to use whenever possible, and is almost always in modern browsers (or at least will be), let . It's a shame that there is still no guaranteed support for browsers that are in full use.

That is, this is not something specific to try-catch , it is valid for any block.

More information on this question in the SO .

    
23.07.2015 / 15:09