Home > Web Front-end > JS Tutorial > Principles of JavaScript functional programming_javascript skills

Principles of JavaScript functional programming_javascript skills

WBOY
Release: 2016-05-16 18:44:18
Original
1088 people have browsed it
1. Calling functions and methods in JavaScript
In JavaScript, there are two ways to call functions. The general way is to put the parameters in parentheses. Another way is to put both the function and the parameters in parentheses. For example:
Copy code The 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 function
An anonymous function is a function or method without a name. Anonymous functions can be thought of as one-shot functions. They are especially useful when you only need to use a function once. By using anonymous functions, since there are no relevant references and identifiers, they will be garbage collected after execution, so using anonymous functions is more efficient. Let’s briefly compare anonymous functions with other referenced or identified functions:
Copy code The code is as follows:

function test(x)
{
alert("Define an identification function");
}
var test = function()
{
alert("Will an The anonymous function points to a reference");
}
(function()
{
alert("I am an anonymous function");
})();//This is actually already Defined and executed an anonymous function

Most languages ​​support using functions as operands (parameters) to participate in operations. However, due to the different positioning of the functions, their operation results are not the same. 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 code The code is as follows:

function test(func)
{
alert(func);
}
test((function(){return "Anonymous function (execution result) as parameter"})());

Functional programming Every variable is created temporarily. Or you can think of it this way: There is no concept of variables in functional expressions. Any data is calculated according to certain rules (functions) based on actual needs. This also solves the problem of concurrent access to atomic variables to a certain extent.
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template