1. Review what an object is and how to create one
Var student = {name: 'c ', age: 23, study: function() {console.log(' ${this.name}'); } } student.study();Copy the code
2. Review basic data types
var a = 10; var b = a; a = 50; console.log(b); / / 100Copy the code
3. Review reference data types
Let arr = [" xiaohong ", "dark horse "," Silicon Valley "]; let brr = arr; Arr. Push (" feng "); console.log(brr); //Array(4) [" black ", "black "," Black ", "black "," black "]Copy the code
As you can see from the above example, basic datatypes are direct assignments, whereas reference datatypes have their own address and point to the same address
4. Shallow copy of objects (only copy the value of the first layer) three methods
Let obj1 = {name: "age ", age: 20,} let obj2 = {} for (let key in obj1) { Obj2 [key] = obj1[key]} obj1.name = "smartyou group "; console.log(obj1); console.log(obj2); console.log(obj2 === obj1); ES6 extend operator let obj3 = {name: "huajun ", age: 20,}; let obj4 = {... obj}; Obj3. name = "vision" console.log(obj3); //Object {name: "vision ", age: 20} console.log(obj4); //Object {name: "huachen ", age: 20} console.log(obj3 === obj4); Object.assign() let obj5 = {name: "", age: 20,} let obj6 = object.assign ({}, obj5); Obj5. name = "education "; console.log(obj5); console.log(obj6); console.log(obj5 === obj6); //falseCopy the code
5. Deep copy of object (both layer 1 and layer 2 can be copied)
Let oldObj = {name: "xiaoming ", age: 20, grade: [10, 20, 30, 40], family: {fName: } // Deep copy // note: Write an Array, Object let newObj = {} function deepClone(newO, Old) {for (let key in old) {let value = old[key] if (value instanceof Array) {newO[key] = [] DeepClone (newO[key], value)} else if (value instanceof Object) {newO[key] = {} deepClone(newO[key], value) } else { newO[key] = value; } } } deepClone(newObj, oldObj) oldObj.grade.push(600); Name = "console.log" (oldObj); //Object {name: "red ", age: 20, grade: (5) [...] Family: {... } } console.log(newObj); //Object {name: "小 小 ", age: 20, grade: (4) [...] Family: {... } } console.log(oldObj === newObj); //falseCopy the code