Simple data types and complex types in JS

1. Simple type and complex type

  • Value type: The simple data type is also called the basic data type. The value itself is stored in the variable when it is stored, so it is called the value type.

  • Including five categories: string, number, boolean, undefined, null.
    Note that null is a bit special. It returns an Object instead of itself.

 // 简单数据类型 null  返回的是一个空的对象  object 
    var timer = null
	console.log(typeof timer)
// 如果有个变量我们以后打算存储为对象,暂时没想好放啥, 这个时候就给 null 
  • Types are also known reference complex data types, only the memory address (reference), hence the name cited data type
  • Objects created by the new keyword , such as Object, Array, Date, etc.

2. Heap and stack

  • Simple data types are stored in the stack and directly open up a space to store the value
  • The complex data type first stores the address in the stack in the hexadecimal representation, and then this address points to the data in the heap, which is actually placed in the heap.

Insert picture description here

Three. Simple type parameter transfer

  • The formal parameter of a function can also be regarded as a variable. When we pass a value type variable as a parameter to the formal parameter of the function, we actually copy the value of the variable in the stack space to the formal parameter, then inside the method Any modification to the formal parameters will not affect the external variables.
	function fn(a){
    
    
		a++;
		console.log(a)
	}
	var x = 10;
	fu(x);
	console.log(x);

Four. Complex types pass parameters

  • The formal parameter of a function can also be regarded as a variable. When we pass a reference type variable to the formal parameter, we actually copy the heap address of the variable stored in the stack space to the formal parameter. The formal parameter and the actual parameter are actually stored The same heap address, so the operation is the same object.
    Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46978034/article/details/109444813