为什么全局变量在函数内部未定义?

时间:2019-08-13 10:36:24

标签: javascript

我制作了var display_val = document.case.display.value;,此变量在函数内部称为:

run0() {
    var display_val = document.case.display.value;
    display_val  += "0"
    queue.push('0')
};

我的代码不起作用,控制台显示以下错误:

  

未捕获的TypeError:无法读取undefinedat的属性“ display”   main.js:72

js:72 var display_val = document.case.display.value;

完整的html代码

<html>
<head>
    <!--Copyright 2019, Aleksa Kovacevic, All rights reserved.-->
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="Online calculators for everything. Some solve problems, some satisfy curiosity." />
    <meta name="keywords" content="calculator, mortgage, loan,lease, cooking, math, college tuition, agriculture, finance,fractions,love,scientific, design, health, unit converter, pocket, running, calculators" />
    <link rel="icon" href="https://www.apkmirror.com/wp-content/uploads/2017/11/5a0aad10ea5ec.png">
    <title id= "Title">Calculator </title>
    <link href="https://fonts.googleapis.com/css?family=Inconsolata&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">
     <link rel="stylesheet" href="main.css" type="text/css">
     <link rel="stylesheet" href="Notiflix\node_modules\notiflix\dist\notiflix-1.8.0.min.css" />
     <script src="Notiflix\node_modules\notiflix\dist\notiflix-aio-1.8.0.js""></script>

        <script src="main.js" type="text/javascript"></script>
</head>
<body>
<button type="button" id="Git" onclick="Git()"> GitHub</button>
<div id="wrapper">
<form name="case" > <!--Buttons -->
      <input name="display" id="display" placeholder "0" onkeypress="" autofocus readonly>
    <input type="button" class="oper" value="(" onclick="runLB()">
    <input type="button" class="oper" value=")" onclick="runRB()">
    <input type="button" id="back" 	class="oper" 	value="CE" onclick="runBack()">
   <input type="button" id="divide" 	class="oper" 	value="÷" onclick="runDivide()" >

    <input type="button" class="digit" value="1" onclick="run1()">
    <input type="button" class="digit" value="2" onclick="run2()">
    <input type="button" class="digit" value="3" onclick="run3()">
    <input type="button" id="multiply" 	class="oper" 	value="×" onclick="runMultiply()">

    <input type="button" class="digit" value="4" onclick="run4()">
    <input type="button" class="digit" value="5" onclick="run5()">
    <input type="button" class="digit" value="6" onclick="run6()">
    <input type="button" id="minus" class="oper" 	value="-" onclick="runMinus()" >

    <input type="button" class="digit" value="7" onclick="run7()">
    <input type="button" class="digit" value="8" onclick="run8()">
    <input type="button" class="digit" value="9" onclick="run9()">
    <input type="button" id="plus" 	class="oper" 	value="+"  onclick="runPlus()">

    <input type="button" class="digit" value="0" onclick="run0()">
    <input type="button" id="comma" class="digit" value="." onclick="runComma()">
    <input type="button" id="equal" 	class="oper"	value="=" onclick="runEquals()">
<div id="Cal">
<textarea  id ="TE" placeholder="Note"></textarea>
</div>
<div id="newpos">
    <!-- button rainbow -->
<button type="button" id="Note" onclick="myFunction()"> Note</button></div>

</form>
<div id="new">
<!--result textarea-->
<textarea  id="result" placeholder="History" readonly></textarea>
<button type="button" id="Del" onclick="Del()"> Delete</button>
<button type="button" id="Print" onclick="printTextArea()" > Print</button>
<button type="button" id="FP" onclick="FontP()" >Font +</button>
<button type="button" id="FM" onclick="FontM()" >Font -</button>
<button type="button" id="SaveBtn" onclick="SaveBtn" >Save</button>
</div>
</div>
</body>
任何信息都会帮助我

1 个答案:

答案 0 :(得分:-2)

display_val是全局变量。

要在函数内部使用它,必须使用this.display_val而不是var