JS learning notes

JS learning notes

Similarities and differences between js and java

First, the syntax of js is somewhat similar to that of kottlin. For example, var is used for method declaration

function method name (parameter name...){ // Method internal logic }

There are also variable type declarations:

Data type: variable name = value

Differences: 1: the data type of js is similar to that of java, except that the data type number in js integrates int, double and float in java. 2: js does not need to declare a variable type. If a variable does not declare a data type, its type depends on the data type of the current value. For example:

var num=0;
num-"lyyyyyyyyyyyyyy";
num=[];
num={};

3: Type judgment in js:

	Judge the basic type and return a string
console.log(typeof '');//string
console.log(typeof []);//object
console.log(typeof {});//object
console.log(typeof 1);//number
console.log(typeof null);//object
console.log(typeof undefined);//undefined
console.log(typeof true);//boolean
console.log(typeof function(){});//function
console.log(typeof /\d/);//object
	Check which constructor an object belongs to and return true/false
function A(){};
function B(){};
let a = new A();
console.log(a instanceof A);
console.log(a instanceof B);
console.log([] instanceof Array);//true
console.log({} instanceof Object);//true
console.log(/\d/ instanceof RegExp);//true
console.log(function(){} instanceof Object);//true
console.log(function(){} instanceof Function);//true

Variable declaration

js variable declaration can be divided into three types: var declaration, let and const declaration and function declaration.

Function declaration

doSomething();
 
function doSomething() {
    console.log('doSomething');
}
var doSomething= 2;

What do you think the above will output? TypeError? In fact, the output result is doSomething. This raises our question. When function declarations appear together with other declarations, who shall prevail? The answer is that function declarations are above all else. After all, functions are the first citizens of js.

So, what about the following example?

doSomething();
 
function doSomething() {
    console.log('1');
}
 
function doSomething() {
    console.log('2');
}

What to do when multiple function declarations appear? The output of the above code is 2. Because when there are multiple function declarations, the last function declaration replaces the previous one.

doSomething();
 
var doSomething= function() {
    console.log('doSomething');
}

var doSomething = function() {} This format is called a function expression.

In fact, it is divided into two parts, one is var doSomething, and the other is doSomething = function() {}. Referring to example 1, we can know that in fact, the result of this problem should be typeerror (because doSomething is declared but not assigned, doSomething is undefined).

Variable promotion and function promotion in js

stay js Printed values often appear after variables are operated in undefined In fact, the reason is because js There is a function called variable promotion in. For example:
	var data="lyyyyy";
	getData();
	function getData(){
		//First print
		console.log("data Value is: ", data);
		var	data="yyyyyyy";
		//Second printing
		console.log("data Value is: ", data);
	}

The first printed value is undefined and the second printed value is yyyy

reason:
In execution getData()Method will first promote the declaration of variables to the first step inside the function.
	Then declare the function inside the function (if there is a function inside the function).
	After that, the code will be executed according to the logical sequence inside the method. The first two steps are just declarations!!!
You should already know why there are such results here.

The actual method internal code execution order should be as follows:

	function getData(){
		//1, Declare variable
		var	data;
		//2, Declare the function (if there is a function inside the function)

		//3, Execute in the order of the code
		console.log("data Value is: ", data);
		data="yyyyyyy";
		//Second printing
		console.log("data Value is: ", data);
	}

When you see the execution order of the split code, you are not confused about the result.

Why is there variable promotion

So why does the variable increase?

In fact, like other languages, js goes through the compilation and execution stages. In the compilation stage, js collects all variable declarations and declares variables in advance, while other statements will not change their order. Therefore, in the compilation stage, the first step has been executed, and the second step is executed when the statement is executed in the execution stage.

summary

1.js will promote the declaration of variables to the top of js for execution. Therefore, for this statement: var a = 2; in fact, js will divide it into var a; and a = 2; and promote var a to the top for execution.

2. The essence of variable promotion is that the js engine declares all variables when compiling, so all variables have been declared when executing. 3. When there are multiple variable declarations with the same name, the function declaration will overwrite other declarations. If there are multiple function declarations, the last function declaration will overwrite all previous declarations.

Added by ajpasetti on Wed, 08 Dec 2021 12:35:09 +0200