-1

由于在函数内返回变量,我不断得到“未定义”。

这是代码:

var nloads = 1;
function something(loc) {
    console.log(nloads); // returns 1
}
function changeSection(loc) {
    console.log(nloads); // Returns undefined
    nloads = nloads + 1;
    temp = nloads;
}

它有什么问题?/可能导致问题的原因是什么?

4

2 回答 2

0

请检查此代码图片。

<html>
<head>
</head>
<body>
<script>
var nloads = 1;
function something(loc) {

    alert(nloads); // returns 1
}
function changeSection(loc) {
    alert(nloads); // Also returns 1
    nloads = nloads + 1;
    temp = nloads;
}


</script>
<div style="border:solid red; height: 100px;width: 100px;" onClick="something('f');changeSection('f');">   
</div>

</body>
</html>
于 2012-08-16T06:26:11.240 回答
-1
var nloads = 1;
function something(loc) {
    console.log(nloads); // returns 1
}
function changeSection(loc) {
    nloads = nloads + 1;
    **var** temp = nloads; // I was missing var before declaring the variable
    console.log(nloads);
}
于 2012-08-16T06:05:29.150 回答