Check out a free preview of the full Advanced JS Fundamentals to jQuery & Pure DOM Scripting course:
The "Exercise 5: Creating the instance Operator" 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:

In this exercise, you will write the instance operator as if it was implemented in JavaScript. Be sure to check the proto-chain for the constructor’s prototype. - exercises/js/my_js.js

Get Unlimited Access Now

Transcript from the "Exercise 5: Creating the instance Operator" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Alexis Abril: One final exercise for this suite of prototype base concepts. One of really important thing we need to do especially when you're setting up your own custom chains is check whether or not the object you're dealing with is an instance of something you would expect. Or maybe you have a condition to say if it's a shape throw it into our 2D algorithms.

[00:00:35] If it's a sphere throw it in our 3D algorithms. Whatever you have to do but there's a lot of good reasons to check for instance of if you want to see if something is an array you wanna probably use something like type of a key word like type of isn't going to give us what the actual information that we need.

[00:00:52] But if we have an actual array we can say is a instance of array and this will give us true or false. So we don't want that same functionality here in our case. We're checking to see if our newly created person was constructed from our person constructor. Is a little p or little person an instance of capital P person?

[00:01:20] So this is gonna be our exercise. We wanna createa function called instance of it's going to take two arguments one is the object we want to compare and two is the constructor we want to compare it to is this object has this object being constructed from this constructor not necessarily directly.

[00:01:44] We're going to have to check the proto chain, because is Person an instance of base object? Yes, right? Is Person an instance of array? No, that's one branch, it isn't. This person an instance of big person? Yes, so your gonna have to walk up the proto chain to check a few properties.

[00:02:13] Let you're verbalizing this what does instance of need to do for us.
>> Alexis Abril: You don't have to give me the actual syntax or the actual, just using our little graph over here.
>> Speaker 2: True or false base on whether it's not class. Instance of it was created with a certain constructor.

[00:02:45]
>> Alexis Abril: Yeah, we wanna check if, I kinda wanna draw. If you're gonna walk up the proto chain. So imagine we have little person, little p, which is just an object in space. It's proto property points to person's prototype, that's the first thing it points to. The second, then it's approaching goes on to point to base object.

[00:03:13] What we want to check as we move up the proto chain, is this constructor function? What we expect. So we're passing in a function here big P, Person. When we walk up the proto chain is this prototypes constructor what we expect, if it is then we are an instance, if it isn't keep walking up the proto chain until you find something that matches or until you reach the end of the ptoto chain.

[00:03:46]
>> Alexis Abril: Does that make sense? This method will return a Boolean. It'll be true or false, true if it's found, false if it isn't.
>> Speaker 2: I think you said it backwards. You were saying this prototype's constructor instead of this constructor's prototype.
>> Alexis Abril: Yes, yes, sorry.
>> Speaker 2: So, basically what you're doing is you're walking up the proto chain until the next item in the proto chain is equal to the constructor function.

[00:04:19] Constructor function that's passed in?
>> Alexis Abril: Actually, the var is false because you have to do probably both ways.
>> Speaker 2: But not-
>> Alexis Abril: You do it probably both ways.
>> Speaker 2: No is.
>> Alexis Abril: Okay, fair enough.
>> Speaker 2: Yeah, it can be not there the constructor part.
>> Alexis Abril: Yeah, fair enough.

[00:04:42] Does that condition make sense.
>> Alexis Abril: To everyone? Does that not make sense at all to you?
>> Speaker 3: I don't understand the last part that you said.
>> Alexis Abril: Okay, that's a specific case but we're talking about, when you're checking your conditions you have access to the person you have the function and you have your objects and you have it's wherever it's proto is pointing to.

[00:05:13] So here you can check your constructor.prototype. And compare are those two objects the same, are they pointing to the same place in memory. If they are then where an instance of that it return true, if they're not like let's say it's down here keep walking up the proto chain until those match or if they never match just return false.

[00:05:36] Does that make sense? Yeah, okay.
>> Speaker 2: One way of thinking about it is, if you have a date and I want to see if it is an INSTANCEOF object, I would just keep working up a date instances proto, until it equalled object.prototype.