Object Assign( ) Method to Copy or Merge Objects

Video

JavaScript Notes

JavaScript
    // Object.assign (targets, sources…) method
    // used to copy objects OR
    // to merge objects

    let obj1 = {"arms":true, "armCount":2};
    let obj2 = {"weapons": ['missile launcher', 'reciprocating saw']};
    let obj3 = {"canMove":true, "legs":0, "treads":2};

    let arms = Object.assign( {'hasHands':true, 'arms': false}, obj1);
    console.log(arms);  // returns {'hasHands':true, 'arms': true, 'armCount': 2} (note: 'arms':true overwrites 'arms':false)

    let robot = Object.assign( { }, obj1, obj2, obj3 );   //merges all objects into a new object, called robot
    console.log(robot);