1. Calling of functions and methods in JavaScript
In JavaScript, there are two ways to call functions. The general way is to put parameters in brackets, and the other way is to put both functions and parameters in brackets at the same time. like:
function test(x)
{
alert(x);
}
test("hello");
(test)("hello");
//Equivalent to the following code
(function test(x)
{
alert(x);
})("hello");
//It is also equivalent to the following code
(function (x)
{
alert(x);
})("hello");
2. Anonymous functions
Anonymous functions are functions or methods without names. Anonymous functions can be considered as one-time functions. They are especially useful when you only need to use a certain function once. By using anonymous functions, since there are no relevant references and identifications, they will be garbage collected after execution, so using anonymous functions is more efficient. The following is a brief comparison of anonymous functions with other referenced or identification functions:
function test(x)
{
alert("Define an identification function");
}
var test = function()
{
alert("Point an anonymous function to a reference");
}
(function()
{
alert("I am an anonymous function");
})();//A anonymous function has actually been defined and executed here
Most languages support functions as operational elements (parameters). However, due to different positioning of functions, their calculation results are also not expected. When a function in JavaScript is used as a parameter, it is passed by reference. "Function Parameters" are no different from ordinary parameters, and their results return unique values.
function test(func)
{
alert(func);
}
test((function(){return "anonymous function (execution result) as parameter"})());
Each variable in functional programming is temporarily generated. Or you can think that there is no concept of variables in functional formulas. Any data is calculated according to actual needs and is calculated according to certain rules (functions), which also solves the problem of concurrent access to atomic variables to a certain extent.
In JavaScript, there are two ways to call functions. The general way is to put parameters in brackets, and the other way is to put both functions and parameters in brackets at the same time. like:
Copy the codeThe code is as follows:
function test(x)
{
alert(x);
}
test("hello");
(test)("hello");
//Equivalent to the following code
(function test(x)
{
alert(x);
})("hello");
//It is also equivalent to the following code
(function (x)
{
alert(x);
})("hello");
2. Anonymous functions
Anonymous functions are functions or methods without names. Anonymous functions can be considered as one-time functions. They are especially useful when you only need to use a certain function once. By using anonymous functions, since there are no relevant references and identifications, they will be garbage collected after execution, so using anonymous functions is more efficient. The following is a brief comparison of anonymous functions with other referenced or identification functions:
Copy the codeThe code is as follows:
function test(x)
{
alert("Define an identification function");
}
var test = function()
{
alert("Point an anonymous function to a reference");
}
(function()
{
alert("I am an anonymous function");
})();//A anonymous function has actually been defined and executed here
Most languages support functions as operational elements (parameters). However, due to different positioning of functions, their calculation results are also not expected. When a function in JavaScript is used as a parameter, it is passed by reference. "Function Parameters" are no different from ordinary parameters, and their results return unique values.
Copy the codeThe code is as follows:
function test(func)
{
alert(func);
}
test((function(){return "anonymous function (execution result) as parameter"})());
Each variable in functional programming is temporarily generated. Or you can think that there is no concept of variables in functional formulas. Any data is calculated according to actual needs and is calculated according to certain rules (functions), which also solves the problem of concurrent access to atomic variables to a certain extent.