js learning - variable and its upgrade issues (2)

  1. Naming:
    JS variables are weakly typed in, you can save all types of data, variables do not have the type of value types. Variable name with a letter, $, _ the beginning, followed by letters, numbers, _ ..
	var web = "goodnight";
    let $ = "csdn";
    var _dou = "you" ;
	变量名不能是JS语言关键字,比如true、if、class等。
  1. Variable declaration and assignment:
 	var test;
    test = "lxx";
        // 先声明后赋值
 	var test = "lxx";
        // 声明与赋值的结合
  var web = "good",
      name="bad",
      url="https://find.com";
      console.log(url); 
        // 可以同时声明多个变量
        
   var web = url = name = "good";
   console.log(web);
   console.log(url);
   console.log(name);
        
变量可以更换不同类型的数据。
  var web = "lxx";
  console.log(typeof web); //string
  web = 87;
  console.log(typeof web); //number
  web = {};
  console.log(typeof web); //object
        // 变量可以更换不同类型的数据。
  1. Variable upgrade:
    run the code, the parser to parse the code to declare variables raised to the front, which is called lift variable.
    Example: The following code is found in the third line of code while parsing can not be used as variable names, not to perform link error appeared, and not the output of the second line of code in the console :( understand what the resolution process)
	var a = "lxx";
	console.log(a);
	let while = "bhh";//此行会报错

Variable lift:

 	console.log(a);
    var a = 1;
    console.log(a);
    //执行结果会在控制台输出 undefined和1
       
    //以上代码在解析时执行过程:(把声明的语句会提前,赋值的语句位置不变)
    var a;
    console.log(a);//undefined
    a = 1;
    console.log(a);//1           

Console Output:
Here Insert Picture Description
The second example is executed hd :( function, will improve if var within, so console output will still undefined, if removed will result in different output part)

    var dou = "fff";
    function hd() {
    	if (false) {
        	var dou = "xxxxxxxxxx";
        }
    	console.log(dou);
    }
    hd();

result:
Here Insert Picture Description

  1. TDZ:
    TDZ called temporary dead zone . It refers to the variable already exists in scope, but you must let / const can be used after the declaration.

    TDZ can use the program to keep the habit after the first statement, so that the program is more stable.

    In the above variables ascension, variable var statement are not being given an assignment statement after the first use, it will only appear after undefined variable lift, and variables let / const declared dead zone in the presence of a temporary error occurs before using statement :

	console.log(dou);
    let dou = "xxxxxxxxxxx";

Operating results error:
Here Insert Picture Description
generating TDZ in the run function scope, does not allow the use of variables not declared before.

dou = "xxxxxxx";
function run() {
  console.log(dou);
  let dou = "hhhhhhhhhhhh";
}
run();

Here Insert Picture Description
Parameters also there is a problem:
The following code is not declared b assignment does not allow direct use:

function dd(a = b, b = 3) {}
dd(); //Cannot access 'b' before initialization

has been assigned a, b may be used so that a variable, normal access code below:

function dd(a = 2, b = a) {}
dd();
  • After the first use of a variable declaration
  • Recommended let / const and less use var

Learning Resources:
Learning Web site (backing people): HTTPS: //www.houdunren.com/
b station video: https: //www.bilibili.com/video/av80536021

Released four original articles · won praise 1 · views 95

Guess you like

Origin blog.csdn.net/qq_43812504/article/details/104655881