JavaScript anonymous function usage analysis_javascript skills
The examples in this article describe the usage of JavaScript anonymous functions. Share it with everyone for your reference. The details are as follows:
1. Define a function
In JavaScript, a function can be defined through "function declaration" and "function expression", such as
1. Define a function through "function declaration"
function t1(){}
2. Define a function through "function expression"
t2 = function(){}
But the effects of defining functions in two ways are different
t1 is a function declaration. During ‘lexical analysis’, AO.t1 = function(){},-------------it will play a role in the ‘lexical analysis’ stage
t2 is an assignment operation. During 'run', AO.t2 = function(){}, the value is the result returned by the expression on the right, ------it only comes into play in the 'run' stage
2. Anonymous function
In JavaScript, statements in parentheses () are executed as expressions. As mentioned above, you can use "function expressions" to define a function. Then, we can define a function within (), such as
(function t3(){alert(' i am t3');})
If the function does not use a name, modify it as follows
(function(){alert(' i am t3');})
Since the statement contained in () is an expression, it has a return value. The return value of (function(){alert(' i am t3');}) is the defined function, which can be called immediately, such as
(function(){alert(' i am t3');})()
Therefore, define a function without a name within parentheses (), which is called an anonymous function. In this way, anonymous functions are executed immediately without polluting the global situation, which is called immediate execution of function expressions.
3. jquery is an anonymous function
The code of jquery is encapsulated in an anonymous function. This is the outermost code of jquery:
(function(window,undefined){})(window);//立即调用
But why does jquery pass window but not undefined?
Answer: The purpose of passing window is to speed up search and reduce the time of querying variables. For example, the following js code
function(){ function(){ function(){ function(){ document.getElementById(); //这个document将会沿作用域层层上找,直到最外层window全局。 } } } }
In order to speed up the internal search of local variables, jquery directly passes the window in as a parameter, so that the window is on the internal AO of jquery.
The reason for not passing undefined is for safety, because in lower versions of IE and FF, undefined can be reassigned, such as undefined=3;
Declare the local variable undefined (the name is undefined), and at the same time, without passing parameters, the value will naturally be undefined
I hope this article will be helpful to everyone’s JavaScript programming design.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

WPS is a commonly used office software suite, and the WPS table function is widely used for data processing and calculations. In the WPS table, there is a very useful function, the DATEDIF function, which is used to calculate the time difference between two dates. The DATEDIF function is the abbreviation of the English word DateDifference. Its syntax is as follows: DATEDIF(start_date,end_date,unit) where start_date represents the starting date.

How to use the exit function in C language requires specific code examples. In C language, we often need to terminate the execution of the program early in the program, or exit the program under certain conditions. C language provides the exit() function to implement this function. This article will introduce the usage of exit() function and provide corresponding code examples. The exit() function is a standard library function in C language and is included in the header file. Its function is to terminate the execution of the program, and can take an integer

The ISNULL() function in MySQL is a function used to determine whether a specified expression or column is NULL. It returns a Boolean value, 1 if the expression is NULL, 0 otherwise. The ISNULL() function can be used in the SELECT statement or for conditional judgment in the WHERE clause. 1. The basic syntax of the ISNULL() function: ISNULL(expression) where expression is the expression to determine whether it is NULL or

How to use Apple shortcut commands With the continuous development of technology, mobile phones have become an indispensable part of people's lives. Among many mobile phone brands, Apple mobile phones have always been loved by users for their stable systems and powerful functions. Among them, the Apple shortcut command function makes users’ mobile phone experience more convenient and efficient. Apple Shortcuts is a feature launched by Apple for iOS12 and later versions. It helps users simplify their mobile phone operations by creating and executing custom commands to achieve more efficient work and

Detailed explanation of distinct usage in SQL In SQL databases, we often encounter situations where we need to remove duplicate data. At this time, we can use the distinct keyword, which can help us remove duplicate data and make the query results clearer and more accurate. The basic usage of distinct is very simple, just use the distinct keyword in the select statement. For example, the following is a normal select statement: SELECTcolumn_name

Usage of Transform in CSS The Transform property of CSS is a very powerful tool that can perform operations such as translation, rotation, scaling and tilting of HTML elements. It can dramatically change the appearance of elements and make web pages more creative and dynamic. In this article, we will introduce the various uses of Transform in detail and provide specific code examples. 1. Translate (Translate) Translate refers to moving an element a specified distance along the x-axis and y-axis. Its syntax is as follows: tran

The use of POST requests in PHP is a common operation in website development. Data can be sent to the server through POST requests, such as form data, user information, etc. Proper use of POST requests can ensure data security and accuracy. The following will introduce the correct usage of POST requests in PHP and provide specific code examples. 1. Basic principles of POST requests in PHP In PHP, the data submitted through the POST method can be obtained by using the $_POST global variable. The POST method converts the form number into
