How to call function and closure function in JavaScript
5 minute(s) read
|
Published on: Jun 04, 2021
Updated on: Dec 14, 2021
|
There are many programmers, each of whom can master different languages to create the best possible different programs that the user can use. One of the programming languages that are very popular among programmers is the JavaScript programming language, which they use extensively to achieve their goals.
There are various features in this language, and among all of these features, we can mention the existence of different functions and closure functions, which we will discuss in the following.
What is a JavaScript-programming language?
It is one of the most popular programming languages available, through which complex features can be easily implemented and enable the user to provide better user interaction.
Call :
It is possible to use this property for different objects by writing only one method, the point that should be mentioned in this regard, is that all the functions in this language are objects.
Consider the following example.
The result of the example will be as follows.
JavaScript
This example creates an object with three properties (firstName, lastName, fullName).
The fullName property is a method:
Rose Kia
Call () Method:
The call method is being used with an owner object as a parameter. The point that should be mentioned here is that this method is predefined in JavaScript. It can help a lot to do coding and other things more quickly. The same method can be used to call another object, now consider the following example for better understanding where it is used to call a first-person.
The result of the example will be as follows.
JavaScript
This example calls the fullName method of person, using it on person1:
Rose Kia
Note also the example below where the second person is called.
The result of this example will be as follows.
JavaScript-Functions
This example calls the fullName method of person, using it on person2:
Kim Kia
This method can also come with arguments, which to understand the meaning of this case better, you need to pay attention to the following example.
The result of the example will be as follows.
JavaScript-Functions
This example calls the fullName method of person, using it on person1:
John Doe, Oslo, Norway
Closure :
Different variables can have a global or local domain in JavaScript. It is possible to give a global variable a local domain with the closure. You may be wondering what Global Variables are. Here's a brief explanation.
Global Variables:
In this type, a function can access all variables defined inside the function and access variables defined outside the function. With the help of the following example where "a" is a global variable, you can find out more about this.
It should be noted that global and local variables are different from each other, and if you want to modify them, you need to modify each one separately.
The result of the example after clicking on the existing button will be as follows. A function can access variables defined outside the function:
Click Me!
25
Note:
It may be interesting for you to know about local and global variables that the lifespan of global variables is longer than that of local variables, and in fact, global variables are alive until you close a page completely, but local variables are not like that, and the lifespan they have is limited to when the function is called, and when the function ends, the variable's life will end.
JavaScript-Nested :
Regarding the functions in JavaScript, it is necessary to state that all existing functions have access to the global domain. Also, as you know, JavaScript supports nested functions, and finally, these functions have access to their high domain.
Consider the following example in this regard.
The result of the example will be as follows.
JavaScript-Closures
I am counting with a local variable.
1
A Counter Dilemma:
When you want to use a variable to count something, and you want the counter to be accessible to all the functions, you can use a global variable and a function to increase the counters. To be able to understand this issue, it is better to pay attention to the following example.
The result of the example will be as follows.
JavaScript-Function Closures
I am counting with a global variable.
The counter is: 3
An example to understand more about JavaScript Closures:
The result of the example will be as follows, where each time a button is clicked, a number will be added to the number shown below the button.
JavaScript-Closures
I am counting with a local variable.
Count!
0
The same example after clicking the button 100 times will be as follows.
JavaScript-Closures
I am counting with a local variable.
Count!
100
Last word:
In general, there are various features in the JavaScript programming language that, if you master each of them, you can use in the best possible way to achieve your goals. In this article, we have examined the function call and closure function, and you have realized that you can use them to implement various features. It should be noted that global and local domains have a great impact on the loading speed of a page which is one of the most important factors of attracting users to the website, so that you can be more successful with the help of them, we hope that this article can be useful for you.
Website SEO analysis services