Transcript from the "Exercise 6 and Solution: Implementing $.extend" Lesson
>> Justin Meyer: I'm going to show you the answer to extend. Again, what we're gonna do is go through every property in object and copy it to a properties on target. To do this, I'm gonna use a for-in loop. So I'll show you how to write one of those. And write for the name of a variable that I want to take on each property name an object, I'm just gonna call it prop.
[00:00:36] So this is gonna be set to, I'll just write out var prop in object. So prop is gonna be set to a string. That is a string for each property name in object. And that's probably different for every loop inside these curly braces, for the body of the for loop.
[00:00:59] So I can read the prop property of object like this. I mean I'll do var value.
>> Justin Meyer: And then I want to set, this is the value of whatever property name proper presents, I wrote this wrong. And then I'm going to set value on target. So I'll do target, for targets prop, I wanna set that to value.
[00:01:34] Does that makes sense? So I'm reading the value for some given property name, and then I'm setting that value on target for some given property name.
>> Speaker 2: [COUGH]
>> Justin Meyer: I broke it up in two lines like this, but most of you have just written it this way, and I think it's a little bit more simple to look at, like that.
[00:01:58] And then I'm just gonna return target.
[00:02:25] A few of you also made sure that the we were only copying the properties directly on object, and not any properties that might be up the proto chain. To do that, people who did this, and this is you don't have to have this in your solution for the example to work.
[00:02:45] It's checking object.hasOwnProperty(prop). So it's making sure that it's only copying properties directly on object, not enumerable properties up the proto chain, and copying it that way.
>> Speaker 3: First letter of your name. [CROSSTALK]
>> Justin Meyer: Yeah, so cool. Thank you.