为什么这个' undefined'

时间:2017-04-09 15:34:51

标签: javascript undefined

我是JavaScript的新手,我只是想了解为什么' z'回复未定义。



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>
&#13;
&#13;
&#13;

如果我按原样运行代码,而不注释任何内容,则结果为:

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

如果我删除了:if(typeof z!==&#39; undefined&#39;),代码结果为:

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

和行:console.log(&#39;外部函数:z =&#39; + z);不执行。我可能这可能是因为函数有一个return语句,但是注释返回并没有改变结果。

任何理解这一点的帮助都会很棒。 感谢您的任何反馈。

4 个答案:

答案 0 :(得分:1)

阅读关于加载/执行DOM的订单的问题的Stack Overflow答案。

您写道:

  

如果我删除了:if(typeof z!=='undefined'),代码结果为[..]   和line:console.log('outside function:z ='+ z);不执行。我可能这可能是因为函数有一个return语句,但是注释返回并没有改变结果。

这是因为您在int main(int argc, char* argv[]){ int count = 0; int collisions = 0; fstream input(argv[1]); string x; int array[30000]; //File stream while(!input.eof()){ input>>x; array[count] = hashcode(x); for(int i = 0; i<count; i++){ if(array[i]==array[count]){ collisions++; // Once we've found one collision, we don't want to count all of them. break; } } // We don't want to check our hashcode against the value we just added // so we should only increment count here. count++; } cout<<"Total Input is " <<count-1<<endl; cout<<"Collision # is "<<collisions<<endl; } 标记加载之前执行functionWithParameters函数会导致p抛出错误只是因为您要查找的document.getElementById("functionWithParameters")标记没有当时存在。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

该函数抛出异常,因此z永远无法获取值。此异常的原因是您在文档加载完成之前尝试访问元素。

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

答案 3 :(得分: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.