recursive
What is recursion?
A function is recursive if it can call itself internally. A recursive function is a function that calls itself internally
Recursive functions function as loops because recursion is prone to “stackoverflow” errors, an exit conditional return must be added.
function fn(num) {
console.log(num);
if (num > 10) {return num;
}
else{
num++;
fn(num);
};
}
Copy the code
Shallow copy Object.assign(newObj, targetObj)
var o = {
id: 1.name: "Car".product: {
id: 2.name: "Refrigerator",}};var obj = {};
Object.assign(obj, o); // copy o shallow to obj
console.log(obj);/ / {id: 1, name: 'cars' product: {id: 2, name:' fridge}}
Copy the code
A shallow copy is only a copy of one layer. When the property inside the object is also an object, the property object assigns the address to the copied object.
Deep copy
var o = {
id: 1.name: "Car".product: {
id: 2.name: "Refrigerator",}};var obj = {};
function deepCopy(newObj, targetObj) {
for (var k in targetObj) {
if (targetObj[k] instanceof Array) { // Since arrays are also objects, we need to put array judgment first
newObj[k] = [];
deepCopy(newObj[k], targetObj[k]);
} else if (targetObj[k] instanceof Object) {
newObj[k] = {};
deepCopy(newObj[k], targetObj[k]);
} else {
newObj[k] = targetObj[k];
}
}
}
deepCopy(obj, o);
console.log(obj);
Copy the code