-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);
                }
            }
4

2 回答 2

0

Java 和 JavaScript 中的变量范围是否相同。

不,但它是相似的。一方面,JavaScript 没有块作用域;Java 可以。例如,在 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

更多:可怜的误解var

我在 javascript 中有一个变量,并在加载页面时分配。但是在我的项目中继续添加函数时,我将此变量设置为 NULL。

听起来您在onLoad运行之前使用了其他功能。该window load事件在页面加载时发生得很晚。

...但很少有地方(在函数中)使用同名的局部变量。我怀疑这会影响我的全局变量值...

如果您正确声明了局部变量,那么这样做会阻止您使用全局变量,但不会更改全局变量的值。这就像 Java(一个映射实例成员的局部变量)。

例如:

var x;
x = 42;

function foo() {
    var x;

    console.log(x); // undefined
}

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

内部与全局不一样x。使用inside使用within 。使用外部使用全局。fooxxfooxfooxfoo

于 2013-11-01T08:08:30.413 回答
0

Java 和 JavaScript 中的变量范围是否相同?

简短的回答:不,变量在这些语言中具有不同的范围。Java 中的变量具有块作用域,而在 JavaScript 中它们具有函数作用域

于 2013-11-01T08:08:36.957 回答