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 7: 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 7.

Get Unlimited Access Now

Transcript from the "Exercise 7: Solution" Lesson

>> [MUSIC]

>> Justin: Again this is to introduce you, a little bit, to reaching into host object functions and calling them for your own purposes. The solution to this is, this is the potential array like thing, or array or not. We're just going to return the result of calling object.prototype.toString. So we're getting object prototypes toString method, but instead of calling it an object, we're going to call it, well, instead of calling it on an, like this object is just toString normally, whatever it might be.

[00:00:39] We're going to make sure we're calling object prototypes toString with our object, and then we're going to make sure that that is equal to object Array. Is that what everybody's got? Let me make sure that that is right, I spelled it right, yes, good. Object Array. Any questions there?

>> Speaker 2: Yes, why does it return arguments if you just say object toString?
>> Justin: If I say object-
>> Speaker 2: I just tried it out just to see if that would work too, and why do you have to use call instead of just directly calling toString?
>> Justin: Instead of just writing object.toString, is that what you mean?

>> Speaker 2: Right.
>> Justin: Because, well let's say I passed in, let's say I made an, I mean one reason is that I could do something like evil and go-
>> Justin: Now this would return true then, if I passed this object, if this was the object I passed.
>> Justin: It would call, if I just did the obj toString, it would return the same thing, it'd return true.

[00:02:09] There's a way of making sure no matter what. Also I think, but I'm not a 100% sure right off the top of my head, but I think arrays have-
>> Justin: I think arrays have a different toString. They have their own, on array.prototype, it has its own toString that's different than objects toString method would return.

[00:02:39] So that's partly why as well. Makes sense? [LAUGH] I could go, there's a much longer explanation, but-
>> Speaker 3: Are you just asking instead of calling it on the prototype? Calling it object.toString?
>> Justin: I'm sorry, I misunderstood your question. I thought you were asking, I thought you meant this, but you meant, why not?

>> Speaker 3: Well no, yes, that is what I meant.
>> Justin: Okay. So that's why, it could be faked, this for one. Then also arrays toString, as you can see, array, if I had array. Its toString returns a representation that's joined. So, you can think of it this way. Arrays toString method just does a join and then returns whatever that is, but objects toString method, what that will do is it looks up whatever that special type is.

[00:03:50] It can look up a special type and know the- I forgot there's a term for it in the ECMA Script spec, which is like the, if it's a regular expression or if it's whatever those base constructors are. It'll actually give you the name for it. So it'll give you array back.

[00:04:08] That's why it does it that way.
>> Justin: Good question. All right, so let's go to the next- Let's, I'm going to first make sure, just like hopefully you guys did, make sure my test is working. It is, good. We'll go to the next exercise.