Your code above may not be creating the newRobot expected by the SCT.
const newRobot = Object.assign({...}, robot);
We can use the object literal in the first positional argument, or as you have done, create a temporary object. However then we will have a stray object in memory. The literal in the argument is probably the better way to go.
Your code still uses the wrong variable for the new robot. If you wish to use a temporary variable, make it addFeatures, or something, not, newRobot since that is the variable the SCT is checking.
The stray object in this case is addFeatures since it never gets called again, just sits in memory taking up space. A very minor concern, here, but later on in more complex code it will be something to keep at a minimum. Intermediary variables are useful, though at times we don’t need them. This is one of those times.
Right… ok… what I thought was that in practical sense, these properties maybe share by other objects, and so they should be declare by ‘let’ perhaps, but not for this example I understand.
I was playing around with the last step of this exercise and found two viable solutions. I think the first is what the exercise is going for. Is the second also valid? They seem to produce the same output, but with the keys outputted in a different order.
// declares newRobot object and assigns values to two keys
const newRobot = {
laserBlaster: true,
voiceRecognition: true
};
// copies keys from robot object to newRobot object
Object.assign(newRobot, robot);
console.log(newRobot);
// declares newRobot object and copies keys from robot object to newRobot
const newRobot = Object.assign(robot);
// assigns values to two new keys in newRobot
newRobot.laserBlaster = true;
newRobot.voiceRecognition = true;
console.log(newRobot);
I am mostly curious if Object.assign(robot); is proper syntax. Obviously an error is produced when I write const newRobot = Object.assign(newRobot, robot); because newRobot cannot be used as an argument when it hasn’t been defined yet.
The first one is closer to what I think the task is asking for but see Roy’s comment above about creating a stray object.
The second one we’re creating the newRobot and then later adding new values to it, whereas I think the question is asking us to create them in one step.
Try creating it as you did here:
Object.assign(newRobot, robot);
but with the contents of the object in {} as the first argument rather than newRobot.
We can clone an object and add new key-value pairs, and update existing key-values all in one stroke. This returns a new object and leaves the source object intact. There is good reason why this is built in to vanilla JS.
It’s not always easy to determine which is better, as that is more subjective than objective. In the case above, this is an empty object, though maybe not that obvious. The empty object in the first example is more explicit and less of a head turner to a lesser informed reader. Consider who one’s audience is and then decide if you want to explain, this or simply let it go with the literal and avoid questions.
While experimenting with the example in the lesson I came to strange output see questions in the last comment:
const robot = {
model: 'SAL-1000',
mobile: true,
sentient: false,
armor: 'Steel-plated',
energyLevel: 75
};
// Declare newRobot below this line:
const newProps = {
laserBlaster: true,
voiceRecognition: true
};
const newRobot = Object.assign(newProps, robot)
console.log (newProps); // prints object, because .assign() mutates target object and returns an object. That's fine.
console.log ('------------------------------');
console.log (Object.entries(newProps)); // prints array, because .entires() returns an array. That's fine.
console.log ('------------------------------');
console.log(`newRobot: ${newRobot}`); // Q1: why does it return an array? it's .assign(), it should return an object? Q2: why does it print "object Object" instead of the "key:value" or "key, value" like the other rows?
actually all the results are different if I try to concat a string to them… that’s a little strange to me.
The why of it is likely technical and worth a weekend of digging up the reading. It appears that template literals are not equipped to represent a data structure such as an object, so render the literal representation, [object Object]. We can wrestle the value to a serialized object with a JSON method, .stringify().
The difference is subtle. console.log() streams character data. ${} is a string literal, and not treated as character data. Note that JSON insists upon double quotes on all string values. Keys are actually strings.