Why in this exercise do we have to call the method with () following? Just a few lessons back we were taught that using () following a method call is unnecessary.
I believe the only time we were told that a method didn’t need to be called with parenthesis was during the “Getters” and “Setters” lessons. Therefore, that wouldn’t apply to “Destructured Assignments”.
In the code below, the console prints “stay inside” is that because we referenced the variable or the key? Do they need to be called the same thing? When I play around with changing the name of either it appears to only work if the key, variable and console log are all called the same thing…
I notice in the examples for destructured assignment that the object’s key is not only wrapped in curly braces, but surrounded by spaces as well. When testing, I discovered that this is not essential.
Is it convention to surround the key with spaces when using destructured assignment?
I believe getters & setters are methods but slightly special methods, essentially properties (I think) so the rule of invoking with () doesn’t apply to them
Because on the previous lesson we learn getter and setter that they don’t use parenthesis to call a function. Here we don’t use them, so calling functions need to use parenthesis.
However, the {functionality} in the curly brackets just shorthand for robot.functionality- and thus, robot.functionality.beep(); will log Beep Boop as well.
So my question is, why would writing const {functionality.beep()} = robot result in an error? Shouldn’t it log Beep Boop to the console as well?
Const creates the variable with what is inside of {}. Functionality.beep() isn’t a variable. Pretty sure that’s right…Sometimes confidence wavers on these probs.
model is only a property value, and has no additional properties or behaviors. It is not an object we can poll for other values or invoke any methods on since there are none. It is just, ‘1E78V2’.
The reason we could destructure on functionality is because its value IS an object, with a contained method, as demonstrated.
hi,
Thank you so much for your help . This all makes more sense now.
I would only be able to destructure the functionally object to access beep() and fireLaser()
Notice how we are able translate the functionality of invert() to present execution context (outside of the object)? This is a powerful feature. It means we can put all the functions of our program into method form in an object which is a big step toward modularization. Everything in one object means it is portable and easy to plug in. It all sits in the global namespace so don’t confuse this with actual modules that have their own namespaces. Don’t need to go there, though, so belay.
Our program can have the parent object defined, and only bring necessary methods to the forefront for as long as we need them. Make this happen in function scope and there is no memory footprint. Lodash is a very smart approach.
Did I say, ‘belay’? Bookmark this, and keep moving forward. In time you will see this is a rather trivial example, as important as it is.
Ok, so I hated this lecture really badly!!! :(((((
I followed the second example for the second exercise! So, the example said to write a nested object as such:
“const { day } = vampire.preferences;
console.log(day); // Prints ‘stay inside’”
BUT, when I wrote it similarly to extract the nested method from the ‘robot’ object, I got an error of undefined and null! As such:
“const { beep } = robot.functionality.beep();
console.log(beep);”
And then, when I wrote it as such, it worked!??! Can anybody please explain me why?
“const { beep } = robot;
functionality.beep();”
And why did it print the ‘Beep Boop’ without the console.log???