JavaScript immediate execution function usage analysis

JavaScript immediate execution function usage analysis

We know that in general, a function must be called before it can be executed. As shown below, we define a function and call it.

function fn(){
    console.log(1);
    }
    fn();

The print result is:

If we don't call it, the results we print will never be displayed.
So here we will mention our immediately executed function. The so-called immediately executed function is a function that can be executed immediately without being called.

There are two most common ways to write an immediate execution function:

  • (function(){})()
  • (function(){}())

For example:

 (function fn(){
            console.log(2);
        })()

The print result is:

Printing successful.

The second parentheses in the immediately executed function are equivalent to calling the function. We can also pass parameters to the immediately executed function. Write the parameters we want to pass in the second () as actual parameters.

as follows:

 (function fn(a,b){
    console.log('a+b='+a+b);
 })(1,2)

The print result is:

The second method of using the immediately executed function is basically similar to the first one, so I will not go into details here.
It should be noted that if there are multiple immediately executed functions, they need to be separated by commas, otherwise an error will be reported. At the same time, in the immediately executed function, it can also be written in the form of an anonymous function.

So what are the functions or benefits of executing functions immediately?

Its biggest function is to create an independent scope. We know that there is no concept of private scope in javascript . If some variables are declared in the global or local scope in a project developed by multiple people, they may be accidentally overwritten by other people with variables of the same name. However, the variables in the immediately executed function are all local variables, and there will be no naming conflicts.

This is the end of this article about JavaScript immediate execution functions. For more relevant JavaScript immediate execution function content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript Basics: Immediate Execution Function
  • Analysis of usage of anonymous functions executed immediately in JS
  • Analysis of JS immediate execution function function and usage
  • Detailed explanation of examples of immediately executing functions in JavaScript
  • Detailed explanation of immediately executed functions in JS

<<:  MySql fuzzy query json keyword retrieval solution example

>>:  CSS+HTML to realize the top navigation bar function

Recommend

Solution to multiple 302 responses in nginx proxy (nginx Follow 302)

Proxying multiple 302s with proxy_intercept_error...

The pitfall record of case when judging NULL value in MySQL

Table of contents Preface Mysql case when syntax:...

Example code for implementing hexagonal borders with CSS3

The outermost boxF rotates 120 degrees, the secon...

Specific use of routing guards in Vue

Table of contents 1. Global Guard 1.1 Global fron...

ftp remotely connect to Linux via SSH

First install ssh in Linux, taking centos as an e...

MySQL REVOKE to delete user permissions

In MySQL, you can use the REVOKE statement to rem...

Complete steps to install boost library under linux

Preface The Boost library is a portable, source-c...

Kali Linux Vmware virtual machine installation (illustration and text)

Preparation: 1. Install VMware workstation softwa...

Detailed explanation of TS object spread operator and rest operator

Table of contents Overview Object rest attribute ...

Comparison of the advantages of vue3 and vue2

Table of contents Advantage 1: Optimization of di...

Basic statements of MySQL data definition language DDL

MySQL DDL statements What is DDL, DML. DDL is dat...

How to optimize MySQL performance through MySQL slow query

As the number of visits increases, the pressure o...

JavaScript to achieve dynamic color change of table

This article shares the specific code for JavaScr...