This course is out of date and does not reflect Frontend Masters current standards. We now recommend you take the JavaScript: The Hard Parts course.

Check out a free preview of the full Advanced JS Fundamentals to jQuery & Pure DOM Scripting course:
The "Exercise 9: Solution" Lesson is part of the full, Advanced JS Fundamentals to jQuery & Pure DOM Scripting course featured in this preview video. Here's what you'd learn in this lesson:

Justin walks through the solution to exercise 9.

Get Unlimited Access Now

Transcript from the "Exercise 9: Solution" Lesson

>> [MUSIC]

>> Justin Meyer: So we were implementing dollar each. I'm gonna go through my implementation. And please if you haven't finished, code along with me. So here is the dollar each function. Here is the collection of either it's an object or something array like. And the callback when we call with the with the property or index and the value.

[00:00:28] So the first thing I'm going to do is just check if this is array like.
>> Justin Meyer: [COUGH] If it's array like I'm going to do a for loop, the normal for var i = 0. i is less than collection.length. And then I'm gonna increment i. So that's that I'm gonna go through each item in this collection and call back callback.

[00:00:58] So what I'm gonna do is call back. That's my callback. I'm gonna call it. I'm gonna make it so, I don't think it matters for this example, but I wanna make it so, the this inside this callback is the value at i. So I'm gonna write, var value = collection.

>> Justin Meyer: That's the value there. So I'm gonna call back this value, the index as the first argument and the value as the second argument. And then if this is not a array I'm gonna do a for in loop.
>> Justin Meyer: And I'm going to do a collection. I'm wanna make sure that this collection has this property so I'm gonna do if (collection.hasownproperty(prop).

[00:02:14] Not prop like that, prop like that. So make sure this collection. I'm only gonna be iterating through properties that are directly on the object, not up it's proto chain. And as long as that is true, I'm going to be calling my call back again with the value.
>> Justin Meyer: At of the prop.

>> Justin Meyer: I'm gonna the call the value, the prop is the first argument and the value as the second argument. And then I'm going to return the original collection.
>> Speaker 2: Yes, I corrected one of your templates 128.
>> Justin Meyer: But then it's going to blow away what I just wrote.

>> Speaker 2: I didn't know that.
>> Justin Meyer: No, yes, okay, and. I meant yes. Okay, now it's a good time to go in there and change it.
>> Speaker 2: You were experimenting, line 28 you were experimenting. Yeah, okay So this was (object).
>> Justin Meyer: Okay, cool. Any questions about this?
>> Speaker 3: Yes You're asking the question about the syntax of value property value?

[00:04:01] Why is it that way?
>> Justin Meyer: Value property value. Okay, so what I'm doing is this a function that was passed in and this is the cool thing. Again, so cool about JavaScript is you can pass functions in, it's cool but abstract. You can pass functions in and then you can kind of call them.

[00:04:18] We're making our own iterator function here. What this is doing, this is a function and remember there's the call function on functions which takes his arguments the first argument is the this that we want this function to be called with. And all of the other arguments are going to be this going to be the first argument.

[00:04:42] There's gonna be a second argument this function is called with. Hopefully
>> Justin Meyer: The context of the call doesn't have to be value for our exercise and how we're gonna be using our own version of jQuery. I could put null in here or make a default to window or something like that.

[00:05:05] However jQuery does make the this in its dollar each. The value of every item. So we're kind of being closer with jQuery does here but it doesn't have to be that way for what we're doing today. Yeah. Any other question?
>> Speaker 3: One question is the context of call having to be value?

[00:05:30] Do you see that question?
>> Justin Meyer: That's what I was just trying to answer, the context of call having to be value. Yeah, we're conforming to jQuery's API. You don't have to make it in your exercise for what we're doing today. Just I have to pass something in here so I can pass these arguments [COUGH] cool.

[00:05:51] Everybody there, about? Cool, all right go on to the next one. I think there's two more. Here, three more, there's proxy too I thought, or did we not do proxy. It's there, I can't read.
>> Speaker 2: We're not quite done with that one, are we?
>> Justin Meyer: Did I miss something?

[00:06:12] Let me run my test.
>> Speaker 2: Yeah.
>> Justin Meyer: Passes, when did I, when my I thought you had to check because I was getting problems with just doing the call.
>> Speaker 2: Most of them were passing but there was one that. See it runs nine checks and like three of them were failing.

>> Justin Meyer: When we start the-
>> Speaker 2: I ran into that before I returned to collection.
>> Justin Meyer: Okay, I'll start the next exercise, so to get people started on that and then I'll help you with whatever that was, does that work? Yeah.
>> Speaker 2: Okay, cool.