Tip:
Highlight text to annotate it
X
Hello...alright today we're going to look at apply which is a really really great thing to
know about if you don't know about it and it shouldn't take too long.
I'm sure there's more advanced things you could do with it but just get your feet wet.
I like saying it.
It's quite easy so let's start with it start talking about how could actually what does
that mean how would we apply something on to a Function.
First of all Functions are types of objects just like almost anything else in Flash.
And because Functions are types of objects.
Behind the hood they have a little bit more Functionality which is two Functions.
Which are mostly user which is apply and call.
Now let's look at a a scenario where we could use apply and we're going to look
at it call at a different date.
So let's just take a small sample I created an Array.
And this Array has numbers and I really want to know the smallest and biggest number.
Unfortunately if I go to my math class and try the 'min' or the "man' ar the 'max'
I'll see it's expecting me to send it parameters as direct parameters as numbers.
And although we have numbers in an Array.
We cann't just send our Array inside our 'min' parameter.
We have to have a way around it to be able to call our
'min' Function and send it the Array as the parameters.
Now fortunately for us there is a way by using 'apply' that we talked about.
So by just calling 'apply' so instead of calling then and opening () we're going to call 'apply'.
And let's look at the two parameters that apply expects.
The first parameter the 'apply' expects is the scope of that specific Function.
Now as you probably know already there are two types of Functions there' the Function
that doesn't have a scope which is called a Function.
And there are Functions that do have a scope which are called methods and a way to
differentiate between them a lot of time are basically a Function
that belongs to something is a method.
Well a Functions that doesn't belong to any thing is just the Function.
Now a method in our senario we're not using a method.
But if we were using a method we would want
to make sure that we're keeping the this inside the object the same.
Or if we want to we could change the scope.
By changing the parameter here which we're not going to look into in this video.
In this video we're going to say no because we're working with a Function is
actually a static Function.
Or in other words it has no scope fantastic.
So our first parameter was null because our Function doesn't have a scope.
And the second parameter is exactly what we had hoped it's exactly expecting an Array.
that will have the parameters of the Function.
So if I place here 'a' and close the brackets it's all I had to do set my
Function to accept our Array instead of accepting numbers.
So if I run this Function and I just save it for a second and run it.
We're going to see my 'min' value is coming back.
Exactly as I would hope to which is 1 is definitely the lowest number.
We tried also the 'max' and run
our application again we'll see we're getting 1 and we're getting 99.3
which is definitely all the stuff we were hoping to get.
Now obviously apply you could use it a lot more advanced senarios and one of the great
things and the dangerous things of apply is that the built in compiling errors
you would get don't work.
So say for example if I put hare just a string.
Flash would not detect there is a string in there instead of that we're going to get some sort
run time unexpected thing maybe or that value
is just not going to be treated the way
we hoped to treat it so let's say for example false
and I run it one more time.
Now I'm going to get 0 which is what I hoped to get because it took my boolean value false
converted it to 0 and then we get 0 our smallest value now.
Obviously if you would run our'min' or 'max' directly if I would just call math.min
and sent it the value of false.
Then I would get an error especially if I don't spell it correctly.
I will get an error.
Alright so it's telling me we're expecting a number.
So basically by working with 'apply' the danger in it which I think is the most
fantastic and fun part of it you're avoiding all those complying errors.
So instead of that your going to get run time scenarios whichs
if you're comfortable enough that is definitely the next step you should do.
Because a lot of time you're going to find not a lot but some times you're going to find ways
to use an apply instead of writting a whole Function that creates
and finds the minimum or maximum for example our senario why create a new Function.
All you had to do is just call upon.
So I hope this was helpful and if you enjoyed this please let me know and if
you didn't please let me know as well if you have questions concerns comments for request
please feel free to comment on that.