Functions as Objects


English (Auto-generated)

Hi everyone in this cast. We'll be talking about functions as objects. Now, functions are central to making your code more organized and functional. And what this means is that functions are blocks of code assigned to certain functionality in your applications. So in javascript, functions are a type of object known as a function object. You can work with function objects as though they were just regular objects. Functions may be assigned to objects passed in as arguments to other functions. They can be returned from functions and additionally a function can be passed into an array. So let's take a look at a simple example. I'm going to declare a function called a simple and all this does is it returns the bullying. True. And now let's query the type of simple function using the type of operator. So going to use a console dot log statement and then type of operator to query the data type of the function simple. Right now the type of operator returns a function as a functions are a special type of object called function object. Now the function object inherits from the function prototype object, which in turn inherits from the object prototype object. So the built in function object has predefined properties such as name, length and methods such as call and bind. So let's take a look at a couple of these. Now we can query the value of the name, property of a function using the dot notation for example, let's query the name of the simple function. So console dot log. And out here, I'm going to say simple dot And then the name of the property which is just a name because requiring the value of the name property here and this lets us know that the functions name property has a value of a simple and this correlates to the name of the function. We can also query the number of arguments passed to a function using the length property. So out here we can say console dot log simple dot length and it returns zero. And the function simple has no arguments. Therefore the length property returns zero. Now you can also have user created properties which can be defined on a function but that is not recommended. But let's just add a simple let's just add a simple property to this function for fun. So we will say simple dot category is equal to let's say basic. And this might seem similar to you. You know, when you deal with just objects and you're adding values of properties to an object using the dot notation right now, just as we can add properties to an object, we can do the same two functions as we've done. So we've added a category property to the simple function whose value is the string basic. And if I access the category property on the function, it's going to display its name. So console dot log a simple dot category. And this will return to us. Basic. Now, existing methods on the function object are called bind and apply and we can discuss this in a later cast but it's worthwhile to keep in mind that the functional object has these predefined methods. We can also add user created methods on the function object for example, let's just add a simple method on the simple function which is going to return to us a random number when called. So out here, I'll see, I'll type simple. Got nam jin. Right. So that's the name of the method and you know that a method is a function which is associated with an object. In this case it's the function object. So this is a function. And what this function does is it's going to return to us a simple um you know, is not a simple but a random number. So let's just say let random num is equal to math dot random. So using the built in random method of the math object and then out here, I'm going to return rand value of the random num variable and let's close this function. Right? So I have the Nam jin method of the simple function which returns a random number generated using the math dot random method. And calling this should return a random number and we call, we can call this, we can call our method with a simple dot the name of the method, which is in um jen And then open and close brackets and out here it says random. Okay, because I made a mistake here. Right? So it's a typo and let's see if I can just copy and then edit it here, because it's a just a typo, so it's not random. It's random. And now, if I do a simple dot nam, jen, there you go. So it's returned a random number and you know that the math dot random method returns a random number between zero up to but not including one. So, with that being said, we'll end our discussion here on functions as objects.
10 Views 0 Likes 0 Comments

Did you know that in JavaScript functions are a type of object known as function object!

Leave a comment (supports markdown format)