A brief summary of JavaScript related knowledge points

A brief summary of JavaScript related knowledge points

In order to use the memory space more reasonably, different spaces are allocated for different data

Basic data type

1. Undefined : means no value. Note that the return value of typeof(Undefined) is also Undefined. Undefined can be
assigned to any variable or property, but it does not mean that the variable is cleared, but an additional property will be added.
2.Unll : No value. Note: the return value of typeof(Null) is Object, and the variable with Null value is not an object.
3.Boolean : Boolean type, true or false, true or false, which can be processed by code or Control the flow of the code.
4. Number : Number type, which is convenient for batch processing of the code, and also controls the iteration and loop of the code.
Note: typeof(NaN) and typeof(Infinity) both return number. The structure that NaN participates in any numerical calculation is NaN, and NaN != NaN. Infinity / Infinity = NaN
5.String : String type

Reference data type

1.Object object
2.Array array type
3.Funcation function type

You can use typeof to view the data type of the variable name. The
basic data type is stored in the stack. The
reference data type is stored in the heap.

Two ways to customize functions:

<script>
function f(){
     
     
}
</script>

Function will be advanced when called, so f() can be before or after the function

<script>
var fn = funcation(){
     
     
}
</script>

When the fn variable name is advanced, the function body will not be advanced, so when calling,
the return value of the function should be behind the function :
1. When the function has no clear return value, the function returns undefined
2. When there is a return value, it returns normally. content

JavaScript itself does not support function overloading : if two functions have the same name, even if the content of the function body is different, the one defined later will override the one defined earlier, and the latter will be called when called.

<script>
function f(){
     
     
console.log(a)
}
function f(){
     
     
console.log(b)
}//名字一样后边会覆盖前边
</script>

JavaScript is divided into two stages when executing code :
1. Pre-compilation

1. The variable with var is promoted, but the variable assignment will not be promoted to the forefront of the code segment
2. The function declared using function is promoted (the function body is promoted) Exception: when the function is repeatedly named, it is only promoted Function body, when the function appears in the judgment condition, promote the function name without mentioning the function body

<script>
var a = 1 ;//定义变量 此内容可挂载在全局执行上下文中和全局对象里边(window)
if ("a" in window)//判断a是否在window里,显然存在 则此内容为true 继续执行if语句中的内容
{
     
     
	function f(){
     
     
	console.log(a);//输出1
	}
	f()
}
</script>

2. Code execution: execute sequentially from top to bottom

Closure

A stack space that cannot be released, and a function that can read the internal variables of other functions.
Since in the JavaScript language, only the internal sub-functions of the function can read the local variables, so the closure can be simply understood as "defined inside the function function".
Purpose:
1. Get the local variables inside the function
. 2. Keep these variables in the memory.
Note that
because the closure will keep the variables in the function in the memory, you can't abuse the closure, which can easily lead to memory leaks and affect web page performance .

<script>
var i = 0 ;
function A(){
     
     
	var i = 10;//10给i赋值
	function x(){
     
     
		console.log(i);
	}
	return x;
}
var y = A();//执行A 返回x 所对应函数的地址给y 此时对应i的值是10
y();//进入y所对应的地址(函数体)显示出i的值为10,因为闭包导致i的值依然存在为10
function B(){
     
     
	var i = 20 ;
	y();//此时调用函数用到的i的值为函数定义处i的值
}
B();//10
</script>

JavaScript data type conversion : (the two sides of the operator need to have the same data type, if inconsistent, the JS interpreter will convert it by itself)
1. Implicit type conversion: silently convert 0, -0, "", undefined, null and above will be directly converted Into false

<script>
	var a = 1 ;
	function b(){
     
     
		if(!a){
     
     //判断的时候a没有值所以是undefined,取反为真,所以继续执行代码
			var a = 2 ;//预提升a的变量名
		}
		console.log(a);//2
	}
	b();
</script>

2. Forced conversion type: parseInt...

Common features of let and const:

1. The declared variable is not promoted
2. It can be used with {} to form a block-level scope
3. The declared variable will not be mounted on the window global object
4. Can not be repeated

Use let to declare variables and const to declare constants (must be assigned when declared)

How the code runs and stores

Guess you like

Origin blog.csdn.net/Fairyasd/article/details/107417144