“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Let’s start with a couple of questions
Typeof determines which types
When to use= = =
When to use= =
The difference between value types and reference types
How to implement deep copy?
1. The value type
let a = 100
let b = a
a = 200
console.log(b) / / 100
Copy the code
1.1 In-depth Analysis
Value type storage space is relatively small, considering the performance and space use, only one value, directly copied over to use
Common value types
String, Boolean, number, Symbol
let a // undefind
const s = 'abc'
const n = 100
const b = true
const s = Symbol('s')
Copy the code
Const const const const const const const const const const const const const const const const const const const must have a value
2 Reference Types
let a = {age: 20}
let b = a
b.age = 21
console.log(a.age) / / 21
Copy the code
2.1 In-depth Analysis
An object or a JSON can be very, very large
- If stored as a value type, it takes up too much storage space
- And when you copy, you might copy very slowly
2.2 Common Reference Types
const obj = {x: 100}
const arr = ['a'.'b'.'c']
const n = null // Special reference type, pointer to an empty address
// Special reference type, but not used to store data, so there is no such thing as' copy, auxiliary function '
function fn (){}
Copy the code
3. Typeof operator
3.1 TypeOF Identifies all value types
3.2 Identification function
3.3 Determining whether to Reference the Type (No subdivision)
4. Handwritten JS deep copy
- Be careful to determine the value type and reference type
- Notice whether it’s an array or an object
- recursive
/** * deep copy */
const obj1 = {
age: 20.name: 'xxx'.address: {
city: 'beijing'
},
arr: ['a'.'b'.'c']}const obj2 = deepClone(obj1)
obj2.address.city = 'shanghai'
obj2.arr[0] = 'a1'
console.log(obj1.address.city) // beijing
console.log(obj1.arr[0]) // a
/** * deep copy *@param {Object} Obj The object to copy */
function deepClone(obj = {}) {
if (typeofobj ! = ='object' || obj == null) {
// obj is null, or is not an object or array
return obj
}
// Initialize the result
let result
if (obj instanceof Array) {
result = []
} else {
result = {}
}
for (let key in obj) {
// Ensure that key is not an attribute of the stereotype
if (obj.hasOwnProperty(key)) {
// Recursive call!!
result[key] = deepClone(obj[key])
}
}
// Return the result
return result
}
Copy the code
5. Variable evaluation – Watch out for some type conversions
For a full conversion, click # JS conversion. I’m serious
5.1 String Stitching
5.2 = =
The operator
5.3 If statements and logical operations
Two step non operation!! The results obtained
5.3.1 Truly variable:!!!!! a === true
The variables of
5.3.2 Services Variable:!!!!! a === false
The variables of
6. Variable type related interview questions
6.1 TypeOF Determines the types
- Identify all value types
- Identify the function
- Determine if it is a reference type (cannot be subdivided)
6.2 Suitable use= = =
When to use= =
6.3 The difference between value types and reference types
If you understand this answer, if you don’t you can scroll up and see