Functional programming, which belongs to a programming paradigm
1 function is the first citizen , can return a value, can also be a parameter of other functions
Console is a function functions
con (v) {
Console.log (v)
}
//Execute is also a function functions
execute (FN) {
fn (1)
}
///Pass the con function as an argument into the Execute function
execute (CON)//1
2 approach to the writing of natural language
Xiao Chi after dinner and then go to Bath can be shown as eat (). Bathe ()
Eat functions function
eat (eat) {
this.e = eat;
return this;
}
Bath functions function
Bathe (bathe) {
this.b = bathe;
return this;
}
var person = eat ("Xiao Chi is eating"). Bathe ("Xiao Chi to take a bath");
Console.log (PERSON.E)//Xiao Chi in the meal
console.log (person.b)//Xiao pool to take a bath
3 Features of functional programming
Anonymous functions, called functions that have no names, are common in functional programming, and sometimes we need to do some of the functions through it (no function), and we'll look at the following by defining a each function:
Custom each functions function each
(arr,func) {
var length = arr.length;
for (var i = 0 I <length; i++) {
func (I,arr[i])
}}
//execute each function, passing an anonymous function as the parameter of the function each
([1,2,3], function (i,v) {
console.log (' key: ' + i + ', value: ' +v);
});
Output content
//key:0,value:1
//key:1,value:2
//key:2,value:3
Corrie: It is the technique of transforming a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function) and returns a new function that accepts the remaining parameters and returns the result.
Defines the Add function and returns a function functions
Add (num) {return function
(x) {return
num + x;
}
}
ADD1 = Add (1)
Console.log (ADD1 (3))//4
Higher-order function: There is a function as a parameter or function within the return of a function, can be called the function of higher order functions, the above each function is considered a higher-order function.
Conclusion
In practical applications, not confined to functional or object-oriented, usually mixed use of both, in fact, many mainstream object-oriented languages are constantly improving themselves, such as the inclusion of some functional programming language features, JavaScript, the two are well combined, the code can be very simple, Beautiful, and easier to debug.