Transcript from the "Type Checking" Lesson
[00:01:31] The issue that I'll show here, so here I've created an iframe. And I'm getting the iframe's array constructor function and I'm saving it as _Array. And I'm creating a new instance of _Array. If I do typeof arr it gives me object as we know arrays are always type of object.
[00:01:54] But if I do arr as instanceof the normal array I get false. So if you're writing code that you want to be able to work with like arrays from other iframes, it can be very tricky to tell if something is an array or not.
>> Justin Meyer: So, yeah, all these things would fail.
[00:02:19] So what jQuery provides is a way that you'll can always know no matter what host, really what you call these iframes are kind of like host environments. It's like a different, it's almost like the constructor functions in that environment. Those array constructor functions are different than the array constructor function in your main window.
[00:02:38] What jQuery provides is a way to tell if the array is actually arrayed no matter what host environment it came from. So to do that, you can do it yourself by doing Object.prototype.toString.call. This is basically stealing Object.prototype's toString method, but calling it on an array instead of an object.
[00:03:04] And that will always give you [object Array], no matter what host environment this array came from. This is like the true way of checking if something's an array or not. So we're gonna implement our version of $.isArray, jQuery isArray. So this should return true if it's passed a normal array but should also return false if it's arguments.
[00:03:31] But it should also return true if you get an array from a different frame, it should also return true.