5.6 Functions are Objets

One of the confusing concepts of JavaScript is that functions are objects. So this circle function that we have here, is actually an object. Let me show you that in detail. So we're doing a circle bridge and look, these are all members of the circle function or circle object. So the purple icons are methods, like bind, apply, and the blue icons are properties. So let's take a look at some of these members in the console. Here in the console, we have circle point name, which returns the name of this function. Then we have circle point lenght which returns the number of arguments In the last video, I told you that every object in JavaScript has a constructor property, and that refers to the function that was used to create this object. Now here's the interesting part, who do you think created this object? Let's take a look, so let's make a circle. constructor so here we have another built-in constructor called function, and when we declare a function using this syntax. Internally, the JavaScript engine will use this function constructor to create this object. So here I'm going to define a constant called Circle1, to separate us from our circle. We then set it to new Function, here our function requires a parameter, radius, so we're adding it here as a string. Now, as a second argument, I'm going to pass the code inside this function. So, to break it up into multiple lines. I'm going to use the back quotes, and then just copy all of this code and put it here, so when we declare a function internally, it's represented like this. Now we can call this circle 1, just like our circle function. So we can create a circle object equal to new Circle1, and pass 1 as the radius. Save the changes, now in the console, let's save circle and look, this is a circle object with these two members. Now let's see some of the methods available in your functions. So I'm going to delete all of this code, Circle point and here we have this call method, and with this, we can call a function. Look at the arguments. The first argument is this argument. Here we need to pass an empty object and this will refer to this object that we're passing here. Then we add our arguments explicitly, so here we have one argument, we pass 1, if we had multiple arguments, we would pass them explicitly like this. So this expression is exactly like the expression on line 11. When we use the new operator, this new operator internally creates an empty object and passes it as the first argument to the calling method. And this object will determine the context of This, so This will refer to this object. Earlier, I told you that if you don't use the new operator, this will default to the global object which is the window. So, if I want to rewrite this expression this way, instead of passing an empty object, I would pass the window. let's go back, so the first argument here, specified the target of this. Now we have another method called apply. This is exactly like the call method, with this we can call a function, but instead of passing all the arguments explicitly, we pass them in an array. So this is very useful because if you already have an array somewhere in your application and you want to pass an array as a second argument to the apply method. So in JavaScript, functions are objects. That's it for this video on objects in JavaScript, we'll see you for a whole next video.