Question about Object.assign()

Hello,
Project Link
This is my code and it works fine. I just wanted to ask something because I could not understand the documentation.:

const robot = {
	model: 'SAL-1000',
  mobile: true,
  sentient: false,
  armor: 'Steel-plated',
  energyLevel: 75,
  test: {
    deneme1: 'deneme1',
    deneme2: 'deneme2'
  }
};

// What is missing in the following method call?
const robotKeys = Object.keys(robot);
//console.log(robotKeys);

// Declare robotEntries below this line:
const robotEntries = Object.entries(robot)

//console.log(robotEntries);

// Declare newRobot below this line:
const newRobotSpecs = {
  laserBlaster: true,
  voiceRecognition: true
}
const newRobot = Object.assign(newRobotSpecs, robot);

console.log(newRobot);
console.log(robot);
console.log(newRobotSpecs);

At task 3 it says:

Now what if we want another object that has the properties of robot but with a few additional properties. Object.assign() sounds like a great method to use, but like the previous examples we should check Object.assign() documentation at MDN.

Declare a const variable named newRobot . newRobot will be a new object that has all the properties of robot and the properties in the following object: {laserBlaster: true, voiceRecognition: true} . Make sure that you are not changing the robot object!

So I as you can see above I decided to create a new object and assign those two new keys and their values to it. Afterwards I used Object.assign() to mix it up with the robot object. The site said that my way was correct but it had me wondering two things:

1- Was it at all possible to add those two keys with their values without creating a new object to reference them from?

2- Is it at all possible not to mutate either the target or source argument? Simply take both of their contents and add them up to to the variable I’m defining them to?

Read mdn’s description - it may be a bit subtle about this but you only need to create one additional object in total (or zero if you already have both objects)

And you’ll obviously be mutating the object you are creating while you create it since you’re doing so in at least two steps. Maybe that’s what you meant that you wanted with your first question.

ie. don’t do this:

const newRobot = 

Which isn’t even new, you already have a reference to it in another variable. Alternatively, pass in an object literal as the value that Object.assign should mutate.

If you wanted to keep the specs, then you could use Object.assign to copy from it instead of into it.

2 Likes

you did the number 3 wrong, it would be:
// Declare newRobot below this line:
const newRobot = Object.assign({laserBlaster: true, voiceRecognition: true}, robot);

the reference is here:

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }

console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }