Advanced JS Fundamentals to jQuery & Pure DOM Scripting Exercise 3: Invoking Functions
Transcript from the "Exercise 3: Invoking Functions" Lesson
>> Justin Meyer: We've done DOTs, which is cool. We had our test passing with the DOT exercise. But, what if we wanted to do something more than just find a property? What if we wanted to find a function and then invoke that function? This is the trick. Because remember one of our rules was Call, right?
[00:00:21] Dot, and then invoking a function, and the context is gonna be whatever is to the left of the DOT.yet Regardless of where that function is found on the chain, right? So we have another exercise. Let's write a DOTCALL. Let's recreate that same functionality but we're gonna use this DOTCALL method.
[00:00:45] So if you have the same two files open, if you have the test page in your browser and your template file open in your IDE, we're gonna use this type of setup, we're gonna create var Person. This is the same construct that you already have. And then we're gonna say, prototype.speak.
[00:01:05] We're gonna add a function onto our newly constructed objects prototypes, proto, object. So a little bit up the chain, we're gonna add a function called speak, and it's gonna console.log. Hello + this.name. And this, this keyword should be the instance of our newly created object. So we're gonna say, var person = new Person('Smith').
[00:01:33] And when we get a person, speak, and then an array of arguments we wanna pass in. In our case, we don't have any arguments we wanna pass in. This should be the same as saying person.speak. These two lines will be equivalent. The part that's grayed out is just the signature, it's our API we wanna meet.
[00:01:54] The big hint here, DOTCALL will set context.
>> Justin Meyer: .call will invoke a function and set this inside that function.
>> Justin Meyer: Does that make sense? Is there any questions on what you see on the screen. Yes.
>> Speaker 2: [INAUDIBLE] can we use our previous function DOT?.
>> Justin Meyer: You totally can, you totally can, good question.