Check out a free preview of the full Rapid Application Development with Code Generation course

The "Dynamically Invoking Methods" Lesson is part of the full, Rapid Application Development with Code Generation course featured in this preview video. Here's what you'd learn in this lesson:

Lukas live codes dynamically invoking methods, including a single invocation, sequenced invocation, mapped invocation, and reduced invocation. The techniques used in this segment will be used as a foundation for the tools created in this course.


Transcript from the "Dynamically Invoking Methods" Lesson

>> The next example that I want to pull up in the primer is the ability to dynamically invoke a method. So I have four methods here, add, subtract, divide and multiply. And in this case we're taking a and b and we're doing something with this so, from here I have defined an action and it has two properties on it.

So it's an action object. It has a funk property for function and params and so it's taking a parameters object. Now the one thing I will point out is that when you're doing dynamic method invocation, you may not always know what your parameter signature is going to be.

As a result, I prefer to put in a single parameter object and then using JavaScript. I like to D structure the parameters that I want to use from the parameters itself. So in this case, I'm passing in a single object, and then I'm pulling a and b off of it performing some calculation and sending it back.

Now we have a single transaction which is just a single action or we have a ledger. So imagine that you're working like a cashier stand or you're doing some kind of math and these different events happen that you need to either multiply, divide, add or subtract. And so the question is, how do we execute a single action.

How can we accomplish that? So, I am going to just delete everything that was there and we will build this out. So, to execute a single action, we can create an execute method. And this takes in a single action property. And then from here, we're just going to call action, funk, action, params.

And so we are able to store a reference to that function and then take a parameter object D structure it and then call it. So in this case if I wanted to execute the single transaction we'll go const result execute, transaction and then from here let's go ahead and realize it's looking for output.

So let me just go ahead and define that real quick. So now, I have my result, and if I go down here we can see that the result of the single invocation was, well let's see here. I want to add 1 and 3, I could update this to be five and, 83, and you'll see here it's 88.

So just this very, very simple idea of we are storing a reference to a function, and then we're storing a reference to the parameter map. And then we are able to essentially call this at some point. And so now I could take this transaction object it stored in a database, pull this out, and then execute it.

So what this represents, and this is really, really important is simple and basic is this is that this represents something that can happen now or it anytime in the future. So now we're starting to play with space and time just a bit. And so the next question is, well, how can we execute an array of actions?

So what would this look like? Well, what I can do is underneath this output, I can go and go ledger for each action and we will from here I'm going to go output. Within this we're going to capture the output of every one of these particular action objects.

So, we're gonna go action. Well, before I do that, so real quick, I can go action.func(action.params) And let me update this from constant let, there we go. And if we go here, what we see is that it's just kind of bunching them together. So let's fix this. So within here, I'm gonna just enter a newline character output, who would have thought that that was case sensitive Now what I've done here and hopefully everybody kind of saw this is like, well, aren't you kind of doing that up here?

And so I realized and this is important that when you are coding, have your spidey sense on so that if you type something you're like, wait a second, I feel like I just did this. Then this is an opportunity to stop and realize like can I optimize this so in this case.

I was able to refer to the smaller function that I defined up here and so on by having fine grained single purpose functions that they do one thing, we're able to composite it together in a much more efficient way. Also something that is very, very important. Now, this is kinda cool, but the problem is that we actually have six different results or four different results in this case.

We have unique distinct results whereas there is back to the clothesline a lot of circumstances where instead of just going through and just kind of outputting stuff Into these individual kind of function executions. We want to contain that into a single collection and ideally, preserve the previous collection.

So I'm gonna go here, ledger and so, we'll just switch this up to map. We're going to go action and then From here, we are going to then go execute, action, okay. Now what we're gonna see here is that map returns a brand new array. So we were able to preserve an array or preserve this order and push it back into a new array which is important.

Last one. Instead of having a collection of transformed objects, you may want to condense this into a single value that is returned. And so this is where reduce comes in. So from here ledger, reduce. And now, the typical parameter signature for reduce is something like ACC for accumulated plus CURR for current.

I tend to deviate after that and so in my case, I'm gonna go with output and action. And I don't have a strong opinion on this if you wanna preserve the original API, I find that it is a lot easier for me when I update the parameters to be a little bit more self documenting.

So we're going to return, Execute, action. And we're going to start with, An empty string, And this was actually a little more verbose than I needed to be. So, I need to go. And then I'm just going to return this We save this, [LAUGH] And now what we have is some sweet string concatenation.

And execute action. This is so, why this is happening? Because guess what we're doing, math operations, and I put an empty string in there. So this is the real result, which is 13. And so, hopefully, question to the audience. On a scale of 1 to 10, how complex was the JavaScript that we just did?

Hopefully, you're not super offended in the sense of like, I came here to learn how to do magic tricks or came here to learn how to do something really complex. And we start out by wax in the car, wax on, wax off painting the fence is that in my opinion that I would say in terms of complexity this is maybe a two or a three.

We're doing some basic string concatenation. We are doing some basic higher order JavaScript functions for each map and reduce. So I'd give this maybe a two or three, so hopefully everybody we're looking at this and we're saying this is approachable, just some basic high order functions. And so, this is it.

This is in terms of JavaScript, in JavaScript complexity. This is as far as we are going. And with that said we are going to take these very basic techniques, and we're going to start to assemble them into some very powerful tools

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now