Transcript from the "Object Default Assignment" Lesson
>> Kyle Simpson: In exactly the same way. Let's go back to what we had where we were doing it with a declarator.
>> Kyle Simpson: In exactly the same way with arrays, if I ended up, for some reason, not returning anything, like undefined or null, what would happen when I try to do a property reference here?
[00:00:30] A type error. And I don't like those type errors. I don't want those type errors. And so the fallback that I would typically use is to provide an empty object, just like with arrays providing an empty array. And I would do the same thing over here, which is provide the empty object.
>> Kyle Simpson: [COUGH]
>> Kyle Simpson: With arrays, you have the whole array illusion, which was like the empty commas thing to skip over them. With objects, you don't need some syntax for that because you just simply don't list the property that you care about. What if I wanted to name the variable the same thing as its source?
[00:01:35] So I was saying var a = tmp.a. If I did that over here, it ends up looking a little bit redundant to have to say b: b or a: a. And so as a result, if the source and the target are the same name, you only have to list it once.
>> Kyle Simpson: This says if there's an a property, grab it and assign it to a variable of that same name.
>> Kyle Simpson: And of course, if you have a default here, that means still go get the a property, assign it to an a variable, but if there is no a property, assign 42 to that a variable instead.
>> Kyle Simpson: So that's a object destructuring shorthand when the property, the source property, and the target assignment are the same name.
>> Kyle Simpson: And of course that only works if the name is a lexically valid identifier. So if your source property was something with spaces in it, that's not gonna work.
[00:02:51] But if it's a simple name like a, it will work just fine.