02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Building Our First Method

Building our first method (without classes) using an anonymous function.

Video Transcript

00:00Let's create a quick object right here again, and we're going to give the our object
00:07We're going to create a new object the same old we're saying new space
00:11object with a big O
00:15Another we have our new object
00:16Let's first put a let's put a property which we already know how to do and we're going to give this property value
00:22Let's say Ben again, and we're going to give we're in the name
00:26Ben as our uppercase and obviously please feel free to use your own name
00:29The only reason I'm using Ben is because I just can't figure out anything else
00:34right now
00:36Let's add let's have done a method into our objects just a random method that says hello world to do that
00:42We're going to be using something that's called an expo
00:46We're going to create a literal function
00:49Instead of actually creating a function as a function
00:52We're going to create our function as a property in the and the way we do that is we set our
00:58Object type we give our name of our function first
01:01So let's say we're going to say we're going to call this hello world in a low world
01:06Well, maybe just a low right because we already said a low world before
01:10We're going to set the value of equal and we're just going to assign a function to our variable
01:15Open up a bracket close a bracket and this time around we're not giving a name to our function
01:19it's an anonymous function that is then fed into our hello as a value and
01:25We're going to put your value of void because well, it's not going to return anything
01:30We're going to open up a bracket. Oh lowercase void flash corrected me
01:36And then to close the bracket for me because I'm working in CS5
01:39Personally, I really hate that I like opening and closing my own brackets
01:43I don't know why I've always
01:46Like to bring the opening I'm enclosing them even with all the everyone so excited about them that they don't have to close their brackets anymore
01:53But I just feel more in control when I close my own brackets. I feel like I know this is the start
01:58This is the end
01:59I don't have to worry about getting confused of brackets because I created them myself
02:03Which is my personal preference?
02:04But if your application is using it most developers like keeping it because it cuts down
02:09Zero dot zero three seconds of their development time, which I really don't think is really important
02:15But that's just me. So we created our function. Let's just put here a little trace of a low
02:22Now if we run our application now, obviously nothing will happen
02:26And the reason nothing will happen is we never executed our function
02:30And by the way, usually just traditionally you would never get an error for this
02:34But what usually when we create an inline function when we assign a function to a variable
02:40We end it with a semicolon and it's kind of like a hint that all this is actually one line of code
02:46It's like all connected together. Is it mandatory? Absolutely not
02:50Does flash care if you put it or not?
02:53Absolutely not as well. So it's not really important. It's just traditionally that's done
02:57Some programming languages will be upset if you don't do that flash is very friendly forgiving when it comes to semicolons
03:04So the last thing we want to do is actually call our Oh, hello
03:06And we just say hello open bracket closed bracket same way that we've called our functions till now
03:12But now our function is not only a function
03:15It's also a method because it belongs to our Oh object
03:18And if we run our app again, we should get hello in our panel in our output panel
03:25So now we've seen what methods are and actually we've seen how we could create methods on the fly using our objects now
03:32Some of you if you have any experience with object oriented programming
03:36You'll know that this is not the way flash recommends really creating functions these days
03:41This is kind of more traditional to doing it in JavaScript or older versions of flash where things are a lot more dynamic. I
03:50believe it's really important knowing these things and
03:53Especially now that we still don't know how to do it the right way or more there's and personally
03:58I don't believe in right or wrong way
04:00But the way that flash or the community of flash is leaning towards which is a much more stricter more
04:07defined way of building functions and
04:10If we look in and kind of this take a step back for a second
04:14Even though we don't really know how to work with our classes
04:16Even though we've been inside of a class all this time
04:18If we just refer to our class for a second
04:21We're gonna see a lot of things that are maybe starting to make sense now
04:25We haven't worked with visual elements yet, but we will a very very shortly
04:30We have your class which is main and as I said before every single type of objects and object and flashes is actually an object
04:38So we have your an object of some sort of a type that we don't really know
04:41It's actually, you know side note. It's an object that we're defining
04:45We're creating our own class our own blueprint in this in our application in our application has a function
04:52But our function main is that a function really? No, it's a method
04:56Why is it a method because it belongs to our class? It's part of our class definition
05:01So although our object as we're creating and the way we know how to code right now
05:06When we're creating our object, we're actually assigning it dynamically and adding to our object after we created it its functions
05:14But the traditional way the more object oriented approach would be in the blueprints
05:19We could define all these different all these different functions that would go into our
05:24object and kind of to connect the dots
05:28If we just click type o and dot we're gonna see all these different methods all these methods
05:34We didn't define them on the fly
05:36Those were part of the base base object class that defined all these different
05:41properties and all these different methods and on the fly created them when we set our new object and
05:47that's why it was so critical for us to set the word new because if we would just put object and
05:53Open and close a bracket
05:55Object is actually a function if we would just call it this way and what does a function return?
06:00It just returns whatever it returns on the other hand if we're asking for a new object flash
06:06the nose that it needs to go into the object class and
06:10fetch all the information that it needs and
06:13Construct a new object a new
06:17Object object really and not object which is a class
06:20This is very very confusing just because the object right here is a class definition
06:25It's the blueprint and the object on this end is actually the outcome
06:30It's the object itself the real tangible
06:34physical object
06:35So if I just to make it easy to understand
06:38We're creating a new class which its type is of an object and that type object is then created into an object
06:46Okay, it's going to be much much easier to understand when we actually get start working with different types of objects
06:51And we stop repeating the word object every time
06:54Which could definitely become a bit confusing

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!