-3

Is scope of variables same in both Java and JavaScript. I have a variable in javascript and assigned when onLoad Page. but while keep on adding functions in my project, I got this variable as NULL. I checked multiple times this variable not assigned again but few places (In functions) used local variables with the same name. I am doubt that this will effect my global variable value. I have now huge functions I don't want to touch them. Please help me to resolve.

Code here: in

 <script>
// other variables goes here.
    var BUSINESS_CODE_CUSEUR = "<%=BusinessLine.BUSINESS_LINE_CODE_GLOBAL_CUSTODY_UK%>";
    var branchId = null;
//on Refresh branch,
function refreshByBranch(){
            branchId = dijit.byId("branchList").attr("value");
         // other functions calling here who ever is depending on branch.
                        searchTeamList(branchId);
            searchCustomer(branchId);
            searchClientRelationshipFsList();
//and so on.
}

//my function is here: calling on an image click.
function showSelectRootCauseDialog(){
    var br = dijit.byId("branchList").attr("value");
    console.info("showSelectRootCauseDialog - branchId:" + branchId);
    console.info("showSelectRootCauseDialog - br:" + br);
// getting null on line 2.
}
 </script>

-- Problem solved here like this. we made changes in every function.

    function refreshByBranch(){
                branchId = dijit.byId("branchList").attr("value");
                var url = contextPath + "/" + servlet + "?cmd_query_for_user=1&branchId=" + branchId;
                originatorUserStore.url = url;
                ownerUserStore.url = url;
                resolverUserStore.url = url;
                searchTeamList(branchId);
                searchCustomer(branchId);
                searchClientRelationshipFsList();
                if(currentBusinessLineCde == gcBusinessLineCde){
                //  var branchId = dijit.byId("branchList").attr("value");
////-- Problem solved here. by making comment. 
                    searchGroupList(branchId);  
                    searchLocationList(branchId);
                    searchClientList(branchId);
                    searchAccountManagerList(branchId);
                }
            }
Chowdappa
  • 1,580
  • 1
  • 15
  • 31

2 Answers2

0

Is scope of variables same in both Java and JavaScript.

No, but it's similar. For one thing, JavaScript doesn't have block scope; Java does. So for instance, in JavaScript:

for (var i = 0; i < someLimit; ++i) {
    // do something
}
console.log(i); // Works, the variable exists outside the loop; it wouldn't in Java

More: Poor misunderstood var

I have a variable in javascript and assigned when onLoad Page. but while keep on adding functions in my project, I got this variable as NULL.

It sounds like you used those other functions before the onLoad ran. The window load event happens very late in the page load.

...but few places (In functions) used local variables with the same name. I am doubt that this will effect my global variable value...

If you declared the local variable properly, doing that will prevent your using the global, but won't change the global's value. This is just like Java (a local variable shadowing an instance member).

For example:

var x;
x = 42;

function foo() {
    var x;

    console.log(x); // undefined
}

foo();
console.log(x); // 42

The x inside foo is not the same as the x global. Using x within foo uses the x within foo. Using x outside foo uses the global.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Is scope of variables same in both Java and JavaScript?

Short answer: no, variables have different scopes in these languages. Variables in Java have block scope, while in JavaScript they have function scope.

aga
  • 27,954
  • 13
  • 86
  • 121