Home Web Front-end JS Tutorial JavaScript anonymous function instance analysis_javascript skills

JavaScript anonymous function instance analysis_javascript skills

May 16, 2016 pm 04:30 PM
javascript anonymous function

The examples in this article describe the usage of JavaScript anonymous functions. Share it with everyone for your reference. The specific analysis is as follows:

Summary:

This article explains the most basic and important thing in JavaScript - functions. The reason why I wrote this article is because I was asked about it during the interview, and it can be regarded as a refresher.

Let’s take an example first. If you understand it, it means you already understand what this article is about.

Copy code The code is as follows:
var f = (function() {

function f() {return 10;}

return f();

function f() {return 20;}

var f = 30;

})();

console.log(f);

Functions are described in advanced JavaScript programming like this - they can encapsulate any number of statements and can be called and executed anywhere and at any time. I introduced strict mode before. Strict mode has some restrictions on functions:

① The function cannot be named eval or arguments
② You cannot name parameters as eval or arguments
③ There cannot be two named parameters with the same name

If the above situation occurs, it will cause a syntax error and the code cannot be executed.

Function definition

Function definitions are divided into three types

1. Constructor

Copy code The code is as follows:
var fun = new Funciton();

2. Common definition

Copy code The code is as follows:
function fun() {}

3. Functional definition

Copy code The code is as follows:
var fun = function() {};

The function fun can be defined in these three ways.

Parameters

The function does not care how many parameters are passed in, nor what data type the parameters are passed in. Even if the function you define only receives two parameters, you do not necessarily have to pass two parameters when calling this function. You can pass one, three or even no parameters. The reason is that the parameters are represented internally by an array. In the function body, you can access the parameter array through the arguments object, for example

Copy code The code is as follows:
function sayHi() {

alert("Hello " arguments[0] "," arguments[1]);

}

Know how many parameters there are by accessing the length property of the arguments object. The length of the function returns the number of parameters of the function.

Note: All parameters are passed by value, and it is impossible to pass parameters by reference.

Functions cannot be overloaded, they can only be rewritten

If two functions with the same name are defined, the name only belongs to the last defined function, for example:

Copy code The code is as follows:

function add(num) {

return num 100;

}

function add(num) {

return num 200;

}

var result = add(100) //300

Note: The function stops and exits immediately after executing the return statement.

Function types

Functions are divided into two types: named functions and anonymous functions. For example, the following famous function

Copy code The code is as follows:
function fun() {

}

If called, only fun() is needed.

Anonymous functions, as the name suggests, have no function names. For example

function() {}

Function calls are called through function names. How to call anonymous functions? One is to assign the anonymous function to a variable and let the variable serve as the function name. The other is to use () to call, such as the following three methods

1. (function() {return;}());

2. (function() {return;})();

3. function() {return;}();

Example:

Copy code The code is as follows:

(function(x, y) {

​​alert(x y);

})(2,3);

//alert(5)


2 and 3 will be passed as parameters to x and y

Let’s talk about the top example. This example involves closures, which will be discussed later

First define a variable f, and then assign it to an anonymous function. It should be noted here that the definition of all variables in the function will be prepended, so the execution order in the anonymous function is

Copy code The code is as follows:

var f = (function() {

var f = 30;

function f() {return 10;}

function f() {return 20;}

return f();

})();


The outer variable f and the inner variable f are not in the same scope (closure), so they have no influence on each other. ​Because the function cannot be overloaded, the external variable f=(function f() {return 20;})();, so the final output is 20.

I hope this article will be helpful to everyone’s JavaScript programming design.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
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.

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

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,

JavaScript and WebSocket: Building an efficient real-time search engine JavaScript and WebSocket: Building an efficient real-time search engine Dec 17, 2023 pm 10:13 PM

JavaScript and WebSocket: Building an efficient real-time search engine Introduction: With the development of the Internet, users have higher and higher requirements for real-time search engines. When searching with traditional search engines, users need to click the search button to get results. This method cannot meet users' needs for real-time search results. Therefore, using JavaScript and WebSocket technology to implement real-time search engines has become a hot topic. This article will introduce in detail the use of JavaScript

What is the difference between lambda expressions and anonymous functions? What is the difference between lambda expressions and anonymous functions? Apr 17, 2024 pm 03:18 PM

Lambda expressions and anonymous functions are both ways of creating anonymous functions in Python, but there are differences. Assignment method: lambda expression returns a function, and anonymous functions must be assigned to variables to be used. Code complexity: A lambda expression can only contain one expression, while an anonymous function can contain multiple statements.

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

See all articles