Home Web Front-end JS Tutorial A problem analysis about javascript anonymous functions_javascript skills

A problem analysis about javascript anonymous functions_javascript skills

May 16, 2016 pm 05:54 PM
anonymous function

An anonymous function is a function without a name. For example:

Copy code The code is as follows:

function (){
alert(' a function');
}

However, the above code will report an error. Firebug prompt: function statement requires a name, that is: the function must have a name.

The strange thing is that if I wrap this unnamed function with a pair of (), no error will be reported. For example:
Copy code The code is as follows:

(function (){
alert( 'a function');
})

(Pay attention to the ()!) that wraps the function. Although this will not report an error, who knows whether the function was declared successfully? Is it because there is no statement at all that the error is not reported? Let’s test like this: let the function execute itself once:
Copy the code The code is as follows:

( function (){
alert('a function');
}())

As you can see, the function is executed, indicating that the function exists.

Similarly, if you remove the () wrapping the function at this time, the previous error will still be reported and the function will not be executed. . .
Copy code The code is as follows:

function (){
alert('a function ');
}()

Is this really so important for () wrapping functions? Can any expert explain the principle?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Usage and characteristics of C++ anonymous functions Usage and characteristics of C++ anonymous functions Apr 19, 2024 am 09:03 AM

An anonymous function, also known as a lambda expression, is a function that does not specify a name and is used for one-time use or to pass a function pointer. Features include: anonymity, one-time use, closures, return type inference. In practice, it is often used for sorting or other one-time function calls.

Analysis of anonymous function application scenarios in Golang functions Analysis of anonymous function application scenarios in Golang functions May 16, 2023 pm 10:51 PM

As a modern programming language, Golang (also known as Go language) has many powerful features. Among them, anonymous functions are a very important concept in Golang and are widely used in various scenarios. In this article, we will deeply analyze the application scenarios of anonymous functions in Golang functions. Event Handler In event handler, anonymous function is a very convenient and practical tool. Custom logic can be passed to the event handler through an anonymous function, such as: funcmain(){bt

How to use PHP7's anonymous functions and closures to achieve more flexible code logic processing? How to use PHP7's anonymous functions and closures to achieve more flexible code logic processing? Oct 21, 2023 am 10:21 AM

How to use PHP7's anonymous functions and closures to achieve more flexible code logic processing? Before PHP7, we often used functions to encapsulate a specific piece of logic, and then called these functions in the code to implement specific functions. However, sometimes we may need to define some temporary logic blocks in the code. These logic blocks do not need to create an independent function, and at the same time, we do not want to introduce too many global variables into the code. PHP7 introduces anonymous functions and closures, which can solve this problem very well. An anonymous function is a function without a name

Python Lambda expressions: abbreviated, concise, powerful Python Lambda expressions: abbreviated, concise, powerful Feb 19, 2024 pm 08:10 PM

pythonLambda expressions are a powerful and flexible tool for creating concise, readable, and easy-to-use code. They are great for quickly creating anonymous functions that can be passed as arguments to other functions or stored in variables. The basic syntax of a Lambda expression is as follows: lambdaarguments:expression For example, the following Lambda expression adds two numbers: lambdax,y:x+y This Lambda expression can be passed to another function as an argument as follows: defsum( x,y):returnx+yresult=sum(lambdax,y:x+y,1,2)In this example

Anonymous functions in PHP8.0 Anonymous functions in PHP8.0 May 14, 2023 am 08:31 AM

PHP8.0 is the latest version of the PHP programming language. One important update is improvements and enhancements to anonymous functions. An anonymous function (also called a closure) is a special type of function that can be created dynamically at runtime and passed to other functions or stored in a variable. In PHP, anonymous functions are crucial for advanced programming and web development. PHP8.0 provides some new syntax and features that make anonymous functions more flexible and easier to use. Some of the updates are as follows: Type declarations for function parameters in PHP8.0,

Can Golang anonymous functions return multiple values? Can Golang anonymous functions return multiple values? Apr 13, 2024 pm 04:09 PM

Yes, anonymous functions in Go language can return multiple values. Syntax: func(arg1,arg2,...,argN)(ret1,ret2,...,retM){//Function body}. Usage: Use the := operator to receive the return value; use the return keyword to return multiple values.

Python Lambda Expressions: Making Programming Easier Python Lambda Expressions: Making Programming Easier Feb 19, 2024 pm 09:54 PM

A python Lambda expression is a small anonymous function that stores an expression in a variable and returns its value. Lambda expressions are often used to perform simple tasks that can be accomplished by writing a separate function, but Lambda expressions can make the code more concise and readable. The syntax of a Lambda expression is as follows: lambdaarguments: expressionarguments is the parameter list received by the Lambda expression, and expression is the body of the Lambda expression, which contains the code that needs to be executed. For example, the following Lambda expression adds two numbers and returns their sum: lambdax,

How to use PHP7's anonymous functions and closures to achieve more flexible and reusable code logic? How to use PHP7's anonymous functions and closures to achieve more flexible and reusable code logic? Oct 24, 2023 am 10:30 AM

How to use PHP7’s anonymous functions and closures to achieve more flexible and reusable code logic? In the world of PHP programming, anonymous functions and closures are very valuable and powerful tools. PHP7 introduces some new language features that make using anonymous functions and closures more convenient and flexible. This article will introduce how to use PHP7's anonymous functions and closures to achieve more flexible and reusable code logic, and provide some specific code examples. 1. Anonymous function An anonymous function is a function without a name. In PHP, you can use anonymous

See all articles