Transcript from the "Exercise 8 Solution" Lesson
>> Justin Meyer: So Steven just to answer your question while we're all working here. He asked, a better question would have been, is ,in, faster than object, than reading the eight property, double equals undefined? [COUGH] Well, first object double equals undefined, you'd wanna maybe make that triple equals, but you can actually have an undefined property which is confusing, but you really can.
[00:00:32] So that wouldn't be a good test either, but I think what you're really asking is, is reading the property value going to be faster than just telling if the property value exists? So, in, just tells you the property exists, doesn't have to go through actually reading the value.
[00:00:50] I doubt there's too much of a performance difference, but I would bet that, in, is gonna be faster than actually going through and reading the property.
>> Justin Meyer: Probably, that would be my guess. And the AON, there is no test for this exercise, it will be tested by the next exercise, when we implement $each.
[00:01:14] Just cuz this is a private function inside JQuery that it doesn't expose. All right, so I'm gonna go through the answer, a lot of you guys were kind of struggling with this, that's okay, we have to go kinda fast today through the exercises, so just do your best to get through it.
[00:01:34] Ask as questions and then follow along with the answer. So, let me write out how I would implement this. So what I would do is I'd first just make sure that the object's length property is typeof,
>> Justin Meyer: Number. [COUGH] Okay, that's good. Otherwise, it's not array-like, so I'd just return false.
[00:02:07] [COUGH] The next thing I'd probably wanna do is check if the object's length is greater than or equal to 0. Well, I just actually, I'll do it, if it's equal to 0, then we're just gonna return true cuz we're not going to really do anything. And then what I would do, is I'm gonna check if the obj.length is greater than 0.
[00:02:42] And if it's greater than 0, let me show you, if it's greater than 0, I'm going to just make sure that that property that I'm looking for, obj.length minus 1, is actually in my object. So I would just return, if (obj.length- 1) is in obj, and then actually I'm gonna delete this, because I wanted it so that if nothing is hit, I'm just gonna return false at the end here.
>> Justin Meyer: Any questions about this or you guys are all writing it out? If you look in the my_jQuery final, if anybody is like just wanting to copy and paste, basically it's the same thing, just laid out on one line instead of with all of these ifs.
>> Justin Meyer: So, walk through it again, I'm just checking that it has a length property, that's a number, it has to have that.
[00:03:54] If the length is 0, it means it's an empty array-like things, so I'm just gonna return true. And then if the length is greater than 0, it could be, maybe someone has a length property of negative a million. Well, that's not an array. But as long as the length is greater than 0, I'm gonna make sure that at least it has a property right at the end.
[00:04:12] I could, if I were doing it trying to be extremely precise, I could go through every single innumerable property and make sure it exists but that would take long. So jQuery, and I think uses this heuristic, that it's gonna have length minus 1 at least, as a property.
[00:04:28] Then otherwise it's gonna return false.
>> Justin Meyer: Someone asks,
>> Justin Meyer: So some questions? Could also be written as return type of like number in object one. Maybe you could, but it wouldn't get this lengthy [CROSSTALK].
>> Speaker 2: Can you repeat the question?
>> Justin Meyer: Yeah,I kinda mumbled through it, sorry. Yes, the question is could this also be written as return typeof, let me write how he kind of wrote it.
[00:05:10] He asked, could this be done as return typeof obj.length === "number" and essentially, this part right here.
>> Justin Meyer: So the only problem with that, is that it wouldn't work if the length was 0 because it would return false. Because 0 minus 1 is negative 1 and there wouldn't be a negative property in an empty array.
[00:05:50] So isArrayLike would actually return false for something that was in fact an array.
>> Justin Meyer: One other probably smart check that this could do, but would just be to actually check is array, and then if that returns true, return true, otherwise check for all of this. But that's not necessary for this example.
[00:06:16] There's another question too, I'm confused about the obj.length- 1 in object part, this part right here. Okay, so what this is doing is an array, let's say you had an array-like thing, the thing that we want to be array-like might have a 0 property and then some value.
[00:06:35] Let's say that this is name of cats or something.
>> Justin Meyer: And then it would have a length property of 2. What this part is doing right here, is it's checking the length, it's subtracting 1 and getting 1. And it's gonna check that this object has a 1 property, and return true if it does.
>> Justin Meyer: Okay that makes some sense, can't we return directly without any else or else if? You could write it kind of like Steven suggested and I showed two examples ago, where you could return without any ifs or else ifs. I just think that this is much easier to read.
>> Justin Meyer: Cool, and by the way just you guys know, like minifiers like Uglify and all of that, will see this code and convert it into one line with ternary operators and stuff like that. So writing ifs and elses and someone is like hey that makes the code too long, don't listen to him.
[00:07:55] Clear code is much better than short terse code, if it means someone can understand it. Okay, ready to go onto the next exercise.