2017-04-09 70 views
-4

我是JavaScript新手,我只是想了解为什么'z'回到未定义状态。为什么这是'undefined'

var z = functionWithParameters(4, 3); 
 
function functionWithParameters(x, y) { 
 
    if (typeof z !== 'undefined') { 
 
     document.getElementById("functionWithParameters").innerHTML = z; 
 
     console.log('inside function: ' + z); 
 
     console.log('inside function: z is a ' + typeof z); 
 
    } 
 
    console.log('before return: z = ' + z); 
 
    return x * y;  
 
} 
 
console.log('outside function: z = ' + z);
<p id = "functionWithParameters"></p> 
 
    <script> 
 
     functionWithParameters(4, 3); 
 
    </script>

如果我的代码运行,没有任何评论,它的结果:

before return: z = undefined 
outside function: z = 12 
inside function: 12 
inside function: z is a number 
before return: z = 12 

如果我删除的:if(typeof运算ž== '未定义'! ),代码结果如下:

inside function: undefined 
inside function: z is a undefined 
before return: z = undefined 
TypeError: document.getElementById(...) is null[Learn More] (from Firefox), 

和行:console.log('outsi de函数:z ='+ z);不执行。我虽然可能这是因为该函数有一个返回语句,但注释返回没有改变结果。

任何帮助理解这将是非常棒的。 感谢您的任何反馈意见。

+0

我的猜测是var提升。因此,'z'没有给出值并且是未定义的。 – Li357

+0

逻辑没有意义。在函数中如何定义一个依赖函数返回的变量? – charlietfl

+0

@charlietfl我不知道这个例子,但它在递归关系算法中很有意义。 – Linek

回答

1

请阅读Stack Overflow回答关于DOM加载/执行顺序的问题。

您写道:

如果我删除的:if(typeof运算ž== '未定义'!),代码结果与[..] 和行:执行console.log('之外的功能: z ='+ z);不执行。我虽然可能这是因为该函数有一个返回语句,但注释返回没有改变结果。

那是因为你之前加载p标签导致document.getElementById("functionWithParameters")抛出一个错误执行functionWithParameters功能仅仅是因为p标签你正在努力寻找当时不存在。

+1

@charlietfl这不是放在这个错误引发的'p'标签之后的函数执行。这是他发布之后发布的脚本,必须在加载“p”标签之前执行。 – Linek

+0

谢谢,我会通读这些信息。 – Tony

+0

谢谢,这很有帮助。它现在是有道理的,我想我应该已经意识到发生了什么。 – Tony

0

您将变量z的值设置为functionWithParameters的返回值。 因此,在函数functionWithParameters返回值 之前,变量z的值未定义。这是 ,因为它在函数functionWithParameters返回值之前没有得到任何值。

console.log不起作用的原因是 ,因为您的脚本有一个活动错误,您的脚本必须停止。

+0

这绝对是正确的答案。 Z =功能“functionWithParemters”的返回值,因此z直到functionWithParemters返回一个值才会定义。 –

+0

所以,在return语句执行之前z是未定义的。在函数内部,由于相同的原因,z显示为未定义。 我还在HTML头标记中加载了javascript。我应该把它装进身体吗? – Tony

+0

@Tony这就是我的答案,你的代码在加载“p”标签之前执行,因此它在第一次函数运行时不存在,一旦你阅读了我在答案中链接的答案,就会理解它。 – Linek

0
TypeError: document.getElementById(...) is null[Learn More] (from Firefox) 

功能是抛出一个异常,那么z永远无法得到的值。造成这种例外的原因是,您正在尝试在文档加载完成之前访问元素。

解决此问题的一个简单方法是将脚本加载到html文件的底部(在</body>之前)。

0

您不理解函数执行流程。

 var z = functionWithParameters(4, 3); 
     //call functionwithParamarters,and run into function body, 
     //at this time the value of z is undefined, 
     //the if statement block is not executed,then output z is undefined. 
     //after call functionWithParameters ,z is 12 
     function functionWithParameters(x, y) { 
      if (typeof z !== "undefined") { 
       document.getElementById("functionWithParameters").innerHTML = z; 
       console.log('inside function: ' + z); 
       console.log('inside function: z is a ' + typeof z); 
      } 
      console.log('before return: z = ' + z); 
      return x * y; 
     } 
     console.log('outside function: z = ' + z); 
     //at this line z is 12 
     functionWithParameters(10, 10); 
     //z still is 12 , calling functionWithParameters dont change value of z 
     //and if statement block is executed.