Home  >  Article  >  Web Front-end  >  Summary on var usage

Summary on var usage

巴扎黑
巴扎黑Original
2017-06-23 10:50:201781browse

  When I was doing the JavaScript tutorial of MOOC, I encountered some problems about var.

One is about the variable declaration in the function. It is found that the program can run normally regardless of whether the var declaration (req1丶req2丶sumq) is used or not. The code is as follows:

 1       <script > 2           function  app2(x,y) 3           { var sum,x,y; 4             sum = x * y; 5             return sum ; 
 6           } 7            req1 = app2(5,6);  //var req1 = app2(5,6); 8            req2 = app2(2,3);  //var req2 = app2(2,3); 9            sumq = req1 + req2;   //var sumq = req1 + req2; 10            document.write("req1的值:"+req1+"<br/>");11            document.write("req2的值:"+req2+"<br/>");12            document.write(req1+"与"+req2+"和:"+sumq);13        </script>

I was a little confused, so I searched online and read some books. "Javascript Language Essence" mentioned: Directly using undeclared variables is called Implicit global variables. This method is originally for the convenience of beginners, and it is intended that variables do not need to be declared before use. However, forgetting to declare variables has become a very common mistake. JavaScript's strategy is to make variables that you forget to declare in advance global variables, but this may make bugs very difficult to find. So the use of global variables should be avoided.

There is another confusion related to var, which was also discussed in the MOOC. The code is as follows:

 1 <!-- 要创建一个运行于无穷循环中的计数器,我们需要编写一个函数来调用其自身。在下面的代码, 2 点击Start按钮,从0开始计数;点击Stop按钮,停止计数。 --> 3     <!DOCTYPE HTML> 4     <html> 5     <head> 6     <meta charset="utf-8"> 7     <script type="text/javascript"> 8           var num=0; 9           var i;                                        //不声明,不会显示错误。.10           function numCount(){11            document.getElementById('txt').value=num;12            num=num+1;13            i = setTimeout("numCount()",1000);           //若为var i ...,则错误。14            }15             function stopCount() {16             clearTimeout(i);17             }18     </script>19     </head>20     <body>21           <form>22           <input type="text" id="txt" />23           <input type="button" value="Start" onClick="numCount()" />24           <input type="button" value="Stop" onClick="stopCount()" />25           </form>26     </body>27     </html>

The above code is correct and can run normally . But when the 9th line of code (var i;) is deleted, the program can still run normally. The problem is the same as mentioned above: when an undeclared variable is used directly, the variable will become a global variable, so it runs correctly . But when the 13th code changes to:

var i = setTimeout("numCount()",1000);

, regardless of whether the 9th line of code is deleted or not, the program runs incorrectly. This is because: In the function scope, variables declared with var are local variables, and variables declared without var become global variables. So after declaring it with var, the variable i is a local variable, so it will cause a program error.

Also in the global scope, variables defined using var cannot be deleted, and variables defined without var can be deleted. That is to say: Strictly speaking, implicit global variables are not real variables, but attributes of the global object, because attributes can be deleted through delete, but variables cannot.​

The above is the detailed content of Summary on var usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn