The data type

Basic data types: number, string, NULL, Boolean, undefined Reference types: Object, Function, Array not explained here.

Null and undefined

Undefined means the definition has no value assigned. Null means definition and assignment, and the value is NULL.

When null is used

1, NULL is a basic type, but when typeof NULL is object. A special type of a value that represents an empty object reference. Assign the initial value of a variable to NULL, indicating that the value to be assigned is an object.

let b = null
console.log(typeof b, b) // object null
b = { name: 'clying' }
console.log(b) // { name: 'clying' }
Copy the code

2. Release garbage objects

b = { name: 'clying' }
b = null
Copy the code

Make objects executed by B garbage and recycled by the browser.

Data, memory, variables

Data: A number stored in memory in binary form and representing certain information.

Memory: Storage space generated after the memory module is powered on. Memory is divided into stack memory and heap memory. The stack memory holds global or local variables. Heap memory holds objects.

Variable: a variable quantity consisting of a variable name and a variable value. Each variable corresponds to a small piece of memory, the variable name is used to find the corresponding memory, the variable value is the data stored in memory.

Relationships: Memory is the space used to store data, and variables are the identification of memory.

Reference variable assignment

Variable changes

Multiple reference variables refer to the same object. One variable modifies the internal data of the object. All other variables see the modified data.

let obj = { name: 'clying' }
let obj1 = obj
obj.name = 'modified'
obj1.age = 12
console.log(obj, obj1)
/ / {name: 'modified' age: 12} {name: 'modified' age: 12}
Copy the code

The obj variable is saved{ name: 'clying' }Obj1 holds the contents of obj, except that the contents of obj are{ name: 'clying' }The address value of. Before modifying the properties, obj and obj1 in memory both point to the same address.

When you modify the name attribute variable of obj, you directly modify the value of the address to which it points. Change the value of the name attribute to ‘modified’. By the same token, adding the age variable to obj1 is adding the age property where obj1 points to memory.

So, the output values of obj and obj1 are the same, {name: ‘modified ‘, age: 12}.

Variables and Functions

let obj = { name: 'clying' }
let obj1 = obj
obj.age = 22
function fun(o) {
  o.age = 12
}
fun(obj)
console.log(obj, obj1)
// { name: 'clying', age: 12 } { name: 'clying', age: 12 }
Copy the code

Execute fun(obj) to assign the value of obj to O. Inside the function, changing the age property of the o variable is equivalent to changing the age property of obj.

Reference variable modification

Multiple reference variables refer to the same object, so that one reference variable points to another object, and the other reference variable points to the previous object.

let obj = { name: 'clying' }
let obj1 = obj
obj = { name: 'dengdeng'.age: 22 }
console.log(obj, obj1)
// { name: 'dengdeng', age: 22 } { name: 'clying' }
Copy the code

Before obj is modified, obj and obj1 both point to the same address value.

{name: ‘dengdeng’, age: 22} obj = {name: ‘dengdeng’, age: 22} Obj1 still points to the previous object, {name: ‘clying’}.

So the output obj and obj1 point to different addresses, so the output is different.

Reference variables and functions

Here’s another example:

let obj = { name: 'clying' }
let obj1 = obj
obj = { name: 'dengdeng'.age: 22 }
function fun(o) {
  o = { age: 12 }
}
fun(obj)
console.log(obj, obj1)
// { name: 'dengdeng', age: 22 } { name: 'clying' }
Copy the code

For obj1, it still points to the previous object and prints{ name: 'clying' }. Obj, after the first change, obj points to{ name: 'dengdeng', age: 22 }The address value of. Execute fun, copy the value of obj, and assign it to o. In this case, the o variable is stored{ name: 'dengdeng', age: 22 }The value of obj has not changed!And then inside the function, the variable O is redirected{ age: 12 }The address value of.

The actual point is shown above. The o variable inside the fun function becomes a junk object and cannot be used at all.

To compare

There may be some confusion: in variables and functions, the value of obj is changed, why in reference variables and functions, the value of obj is not changed?

First of all, we need to understand that when a JS function passes a variable parameter, it passes the value of the variable. Execute fun, which simply assigns the address of obj to the o variable. Among variables and functions, O is directly modified, which is directly the attribute variable of age inside OBJ. {name: ‘dengdeng’, age: 22}} {name: ‘dengdeng’, age: 22}} Inside the function, o points to another object, the address of {age: 12}. But when you point to another object, obj still points to the previous object.