@TOC

Similarities and differences between JS and Java

First, js’s syntax is somewhat similar to Kottlin’s. For example, var, method declarations are used

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. The js data type number integrates the Java data type int, double, and float. Two: js can not declare variable types. 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={};
Copy the code

Three: type judgment in JS:

Determine the base type and return a stringCopy the code
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

Copy the code
Checks which constructor an object belongs to, returning true/falseCopy the code
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

Copy the code

Variable declarations

Js variable declarations can be roughly divided into three types: var declarations, let and const declarations, and function declarations.

Function declaration

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

What do you think will come out of it? TypeError? The output is actually doSomething. Which begs the question, when a function declaration is accompanied by another declaration, which one prevails? The answer is that function declarations are above all else; after all, functions are the first citizens of JS.

What about the following example?

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

What happens when there are multiple function declarations? 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');
}
Copy the code

Var doSomething = function() {}

Var doSomething = function() {} TypeError is declared (because doSomething is declared but not assigned); So doSomething is undefined).

Js variable promotion and function promotion

= Undefined is often used when printing values after operating on variables in JS. The reason is that js has a feature called variable promotion. For example:

	var data="lyyyyy";
	getData();
	function getData(){
		// First print
		console.log("Data value:", data);
		var	data="yyyyyyy";
		// Print the second time
		console.log("Data value:", data);
	}
Copy the code

The first printed value is undefined, and the second printed value is yyyyy.

The reason:Copy the code

The getData() method is executed to raise the declaration of variables to the first step inside the function. Then declare the function inside the function (if there are any functions inside the function). The code is then executed in the order of logic within the method. The first two steps are just declarations!! If you look at this, you should already know why this is happening.

The actual order of code execution within a method would look like this:

	function getData(){
		/ / a. Declare a variable
		var	data;
		/ / 2. Declare a function (if it has a function inside)

		/ / 3. Execute in code order
		console.log("Data value:", data);
		data="yyyyyyy";
		// Print the second time
		console.log("Data value:", data);
	}
Copy the code

Seeing the order in which the code is executed after the split does not confuse the result.

Why is there a variable lift

So why does it happen?

In fact, JS and other languages, have to go through the compilation and execution stage. At compile time, js will collect all variable declarations and declare them in advance, while other statements will not change their order. Therefore, at compile time, the first step is executed, while the second step is executed when the statement is executed.

conclusion

Var a = 2; var a = 2; Js will actually classify it as var a; And a = 2; Two parts, and the var a step is promoted to the top.

2. The essence of variable promotion is actually due to the js engine at the time of compilation, all variables are declared, so at the time of execution, all variables have been declared. 3. When there are multiple variable declarations with the same name, the function declaration overwrites the other declarations. If there are more than one function declaration, the last one overrides all previous declarations.