Why does this work in javascript?

Just now I saw this code:


if(condition){ var xx='sss'; } //do something if(condition){ console.info(xx); } 

Now I'm just wondering why the second if works? How can it access the xx variable since it is a local variable defined in another if ?

+4
source share
7 answers

var in JavaScript is bound to the containing execution context (for example, the entire scope of the function or the entire global scope if var is in the global scope), not . JavaScript does not have (yet) a framework (ECMAScript6 seems to be able to add it using the new let keyword).

The code you provided is equivalent exactly :

 var xx; if(condition){ xx='sss'; } //do something if(condition){ console.info(xx); } 

This is described in Section 10.5 of the specification, which describes what the engine does when entering a new execution context. (This is basically a two-phase process, setting up all the ads first, and then executing the step-by-step code.)

Read more: Bad misunderstood var

+10
source

In JavaScript, an area is tied to closure (the last function block included or by default refers to a window object). When a variable is declared somewhere inside this function block, it is raised at the top of the scope, so essentially the variable exists as undefined, starting at the very top of the scope if it is declared anywhere in the scope.

Think about it as soon as the code starts executing, it scans all the instructions for the declarations and immediately sends the symbol name.

 console.log(x); // undefined console.log(y); // error: Uncaught ReferenceError: y is not defined var x; 

In this case, you can do this to the extreme:

 console.log(x); // undefined, not an error while (false) { if (false) { var x; } } 

even if var x cannot be reached, and at run time will be fully optimized. the motor will raise a variable at the top of the area

hope this helps -ck

useful link: http://www.youtube.com/watch?v=taaEzHI9xyY&feature=youtu.be#t=42m57s

+3
source

var declarations affect the entire scope of the smallest contained function or program. JavaScript is not blocky.

Crock says:

Variable Declarations

All variables must be declared before use. JavaScript does not require this, but it makes the program more readable and makes it easier to detect undeclared variables that can become implied global. Implied global variables should never be used.

var statements must be the first statements in the function body.

Preferably, each variable is assigned its own line and comment. They must be listed in alphabetical order.

 var currentEntry; // currently selected table entry var level; // indentation level var size; // size of table 

JavaScript does not have a block area, so defining variables in blocks can confuse programmers who have experience working with other languages ​​in the C family. Define all variables at the top of the function.

The use of global variables should be minimized. Implied global variables should never be used.

Note that this is changed using let statement , and in the current JavaScript (EcmaScript 5), the variable name in catch blocked by a block.

+1
source

javascript does not have a block scope, so var xx='sss' either locally localized (if your code sample is inside the function) or globally (if your code sample is not contained in the function).

+1
source

JavaScript is a dynamic language that is not always picky about things, such as a variable scope. So this "function" allows you to write code

 if (condition) { var xx = 'sss'; } else { var xx = 'ttt'; } // do something if (condition) { console.info(xx); } 

I would recommend avoiding this, as this makes it difficult to understand your program and the reasons.

0
source

If you declare a variable with var inside a function, the variable may be limited inside that function. The if statement is not a function.

I assume that in this case both if statements are in the same function, and therefore is xx in scope?

0
source

If a variable is declared inside a conditional statement, it is still available anywhere after the declaration in the contained function (or globally if the condition is not in the function). However, it will be undefined if the condition evaluates to false, unless the variable is assigned a value.

If the local variable is specified globally or in another function, a JavaScript error will occur. Depending on your browser, the error may say that the variable is "undefined" or "is undefined". This is different from a variable equal to undefined, as indicated above, because a variable that is undefined can still be referenced.

-one
source

Source: https://habr.com/ru/post/1397725/


All Articles