Summary on var usage
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.
