JavaScript Zero Basic Clearance (13) Simple Data Types, Simple Data Types Passing Parameters, Simple Types and Complex Types, Memory Allocation of Simple Types, Memory Allocation of Complex Types

27 - Simple data types

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        // 简单数据类型 null  返回的是一个空的对象  object 
        var timer = null;
        console.log(typeof timer);
        // 如果有个变量我们以后打算存储为对象,暂时没想好放啥, 这个时候就给 null 
        // 1. 简单数据类型 是存放在栈里面 里面直接开辟一个空间存放的是值
        // 2. 复杂数据类型 首先在栈里面存放地址 十六进制表示  然后这个地址指向堆里面的数据
    </script>
</head>

<body>

</body>

</html>

If we have a variable that we plan to store as an object in the future, and we have no idea what to put it in, at this time, we will store it in the stack for the "null
simple data type", and directly open up a space to store the time-valued
simple data type: it is stored in the In the stack, a space is directly opened to store the time value. For
complex data types, the address is first stored in the stack in hexadecimal, and then this address points to the data in the heap.
Simple data types: stored in the stack, and a space is directly opened for storage. Time value
Complex data type: first store the address in the stack, expressed in hexadecimal, and then this address points to the data in the pair

28-Simple data type parameter passing

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        // 简单数据类型传参
        function fn(a) {
    
    
            a++;
            console.log(a);
        }
        var x = 10;
        fn(x);
        console.log(x);
    </script>
</head>

<body>

</body>

</html>

function fn(a){a++;console.log(a);}
function Person(name){this.name=name;}
function f1(x){console.log(x.name);x.name="张学友’;console.log(x.name);}
function f1(x)【console.log(x.name);x.name="张学友’lconsole.log(x.name);}

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        // 复杂数据类型传参
        function Person(name) {
    
    
            this.name = name;
        }

        function f1(x) {
    
     // x = p
            console.log(x.name); // 2. 这个输出什么 ?  刘德华   
            x.name = "张学友";
            console.log(x.name); // 3. 这个输出什么 ?   张学友
        }
        var p = new Person("刘德华");
        console.log(p.name); // 1. 这个输出什么 ?   刘德华 
        f1(p);
        console.log(p.name); // 4. 这个输出什么 ?   张学友
    </script>
</head>

<body>

</body>

</html>

function Person(name){this.name=name;}
function f1(x){console.log(x.name);x.name=“张学友”;console.oog(x.name);}

JavaScript simple and complex types

1. Simple and complex types

Simple types are also called basic data types or value types, and complex types are called reference types.
Value type: simple data type/basic data type, the value itself is stored in the variable when it is stored, so it is called the value type
string, number, boolean, undefined, null
reference type: complex data type, only the variable stored in the storage time It is an address (reference), so it is called a reference data type
An object (system object, custom object) created by the new keyword, such as Object, Array, Date, etc.

Simple types are also called basic data types, value types, and complex types are called reference fatigue.
Value types: simple data types, basic data types, and the value itself stored in storage variables.
Reference type: complex data types, variables are stored when stored The only time address, the reference data type, the object system object created by the new keyword, the custom object Object, Array, Date;
2. The
difference between heap and stack stack space allocation:
  1. Stack (operating system): automatically by the operating system Allocate and release the parameter value of the function, the value of the local variable, etc. Its operation method is similar to the stack in the data structure;
simple data types are stored in the stack
  2. Heap (operating system): store complex types (objects), which are generally allocated and released by the programmer. If the programmer does not release, the garbage collection mechanism Recycle.
The complex data type is stored in the heap.
Stack: The parameter value of the function and the value of the local variable are automatically allocated and released by the operating system. The operation method is similar to the
heap: the operating system stores complex type objects, which are generally allocated and released by the programmer. The operating system, which stores complex type objects, is generally used by programmers
insert image description here
. Note: There is no concept of stack in JavaScript. Through the method of stack, it can make it easier for everyone to understand some execution methods of code, which is convenient for learning other languages ​​in the future.
There is no concept of stack in JS,

3. Simple Types of Memory Allocation

Value type (simple data type): string, number, boolean, undefined, null
The data of the value type variable is directly stored in the variable (stack space)
string, number, boolea, undefined, null
string, number, boolean, undefined, null
value The data of the
insert image description here
type variable is directly stored in the variable stack space The data of the value type variable is directly stored in the variable stack space

4. Memory allocation of complex types

Reference type (complex data type): Objects (system objects, custom objects) created by the new keyword, such as Object, Array, Date and other
reference type variables (stack space) store addresses, and real object instances are stored in Reference types in heap space
: complex data types, objects created by the new keyword, system objects, custom objects, Object, Array, Date
Object, Array, Date
reference types (complex data types) objects created by the new keyword, System objects, custom objects, Object, Array, Date
reference type variables, the time address stored in the stack space, the real object instance is stored in the heap space
insert image description here
Reference type (complex data type): objects created by the new keyword (system Object, custom object), such as Object, Array, Date, etc.
The address is stored in the reference type variable (stack space), and the real object instance is stored in the heap space.
Reference type: complex data type: object created by the new keyword , system object, custom object Object, ArraymDate
reference type variable, the time address stored in the stack space, the real object instance is stored in the heap space

5. Simple type parameter passing

The formal parameter of the 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 in the method Any modification to the internal parameters will not affect the external variables.

function fn(a) {
    
    
    a++;
    console.log(a); 
}
var x = 10;
fn(x);
console.log(x)

function fn(a){a++;console.log(a);}
var x=10;
fn(x);
console.log(x);
function fn(a){a++;consolelog(a);}
var x=10;
fn(x);
console.log(x);

6. Complex type parameter passing

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 saved by the variable in the stack space to the formal parameter. The formal parameter and the actual parameter are actually saved. is the same heap address, so the same object is operated on.

The formal parameter of the function can also be regarded as a variable. When we pass the reference type variable to the formal parameter, the heap address saved by the variable in the stack space is actually copied to the formal parameter. When the formal parameter and the actual parameter are saved The same heap address, the same object when operating

function Person(name) {
    
    
    this.name = name;
}
function f1(x) {
    
     // x = p
    console.log(x.name); // 2. 这个输出什么 ?    
    x.name = "张学友";
    console.log(x.name); // 3. 这个输出什么 ?    
}
var p = new Person("刘德华");
console.log(p.name);    // 1. 这个输出什么 ?   
f1(p);
console.log(p.name);    // 4. 这个输出什么 ?  

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 saved by the variable in the stack space to the formal parameter.
The formal parameter of the function can also be seen It is a variable. When we pass a reference type variable to a formal parameter, we actually copy the heap address saved by the variable in the stack space to the formal parameter.
In fact, we copy the heap address saved by the variable in the stack space to the formal parameter. Formal parameters, formal parameters and actual parameters actually save the same heap address, so the operation is the same object

Guess you like

Origin blog.csdn.net/weixin_43428283/article/details/124179020