Variables, scope and memory in js

JS variables can contain two different types of data: primitive values ​​and reference values. Primitive values ​​are the simplest data types, namely: undefined, Null, Boolean, Number, String, Symbol. A reference value is an object made up of multiple values. JS does not allow direct access to memory locations, so you cannot directly manipulate the memory space where the object is located.

Dynamic properties:

Primitive values ​​are defined in a similar way to reference values ​​by creating a variable and assigning it a value. But after the variable holds the value, what can be done with the value is very different. For reference values, additions, deletions, modifications, and queries can be performed at any time.

Primitive values ​​cannot have attributes, but adding attributes to primitive values ​​will not cause an error.

let a = "pack"        a.age = 27           console.log(a.name)  /    undefined 

Attributes can be added to the reference value. When using the new keyword, JS will create an instance of Object type

let a = new Object()   a.name = "pack"  console.log(a.name) /  27

Copy value:

         ① In addition to the different storage methods, the original value and reference value are also different when copied through variables. When copying an original value through a variable to a new variable, the original value is copied to the location of the new variable.

let num = 5; let num2 = num1 At this time, the two 5s of num and num2 are completely independent. Both variables can be used independently without interfering with each other.

        ② When assigning a reference value from one variable to another variable, what is copied here is actually a pointer, because the reference value opens up a space in the heap memory, the variable is in the stack memory, and it points to the object stored in the heap memory .

let obj1 = new Object();let obj2 = obj1 ; 

obj1.name = "jack" ; console.log(obj2.name) / jack    In this example, obj1 saves a new instance of the object, then this value is copied to obj2, but at this time both variables point to a Object, when adding the name attribute to obj1, obj2 can also access this attribute. Because they all point to an object.

Pass parameters:

The parameters of all functions in js are passed by value, which means that the value outside the function will be copied to the parameter inside the function, just like copying from one variable to another. If it is an original value, it is the same as copying the original value, and if it is a reference value, it is the same as copying the reference value.

function add(num) { num += 10 ;  return num }   ;    let  count = 20;

let result = add(count) ; console.log(result , count) / 30 , 20 Here, the function add has a parameter num, here the count variable is passed in, then it is equivalent to num copying the value of the count variable, at this time num and count does not interfere with each other, so if the value of num is changed in the function, the value of count will not change.

    function add(obj) {
        obj.name = "jack"
    }
    let person = new Object()
    add(person)
    console.log(person.name) //"jack"

This time we create an object and save it in the variable person, and then call add(). At this time, the address saved by the person variable will be copied to obj. When changing the properties of obj in the function, the person variable outside the function will also reflect this change. Because the object saved by the obj variable is saved in the heap memory of the global variable. Both obj and person point to this object.

Determine the type:

The typeof operator is most suitable for detecting whether a variable is a primitive value, but we usually don't care whether the value is an object, but want to know what type of object it is. JS provides the instanceof operator.

    console.log(person instanceof Object) // person是Object数据类型吗?
    console.log(person instanceof Object) // person是Object数据类型吗?
    console.log(pattern instanceof RegExp) // pattern是RegExp数据类型吗?

Guess you like

Origin blog.csdn.net/var_infinity/article/details/125852330