with it ECMAScript Standard specification constantly updated, has now been updated to ES7, soon ES8 specification will be available, in order to be JS The syntax is more friendly to function programming, such as RxJS (Reactivex), the constant popularity of functional frameworks . Functional programming should be a function as the carrier body, and then split the function to encapsulate, more abstract, very extensible.
What are the advantages compared to traditional command-style functions?
- Simplified syntax and clarity
- Better versatility
- Better Maintenance and scalability
- Limit scope
The following enumeration functions compare
//each word in the array, capitalized in the first letter//General WordingConst ARR = [' Apple ', ' pen ', ' Apple-pen ']; for(CONST Iincharr) {Const C= Arr[i][0];arr[i]= C.touppercase () + arr[i].slice (1);} Console.log (arr);//Functional Formula OnefunctionUpperfirst (word) {returnWord[0].touppercase () + word.slice (1);}functionWordtouppercase (arr) {returnArr.map (Upperfirst);} Console.log (Wordtouppercase ([' Apple ', ' pen ', ' Apple-pen ']));//functional style notation twoConsole.log (Arr.map ([' Apple ', ' pen ', ' apple-pen '], Word = word[0].touppercase () + word.slice (1)));
When things get more complicated, there are several problems with the way expressions are written:
- It's not obvious, it's becoming difficult to maintain.
- Poor reusability, resulting in more code volume
- will produce many intermediate variables
functional programming is a good solution to the above problems. First of all, see functional notation one , which uses the function of encapsulation to do the function of disassembly (granularity is not unique), and encapsulated as a different function, and then use the combination of calls to achieve the purpose. This makes the table meaning clear, easy to maintain, reuse, and extend. Second, using higher-order functions ,array.map instead of for...of to do array traversal, reducing the intermediate variables and operations.
The main difference between the functional notation and the functional notation two is that it is possible to consider whether a function is subsequently reused, and if not, the latter is better.
Chain-Optimized
from the top In the functional notation two we can see that the functional code in the process of writing, it is easy to create horizontal extension , that is, to produce multiple layers of nesting, below we give an example of the more extreme point.
// calculate the sum of numbers // General Wording Console.log (1 + 2 + 3-4)// functional notation function sum (A, B) {
return A +
b;}
function
Sub (A, b) {
return A-
b;} Console.log (Sub (sum (SUM (1, 2), 3), 4);
//optimized notation (well, you're right, that's Lodash's chain)Const UTILS ={Chain (a) { This. _temp =A; return This; }, sum (b) { This. _temp + =b; return This; }, sub (b) { This. _temp-=b; return This; }, Value () {const _TEMP= This. _temp; This. _temp =undefined; return_temp; }};console.log (Utils.chain (1). SUM (2). SUM (3). Sub (4). value ());
This example is for display only In the extreme case of horizontal stretching , as the number of nested layers of functions increases, the readability of the code is greatly reduced and errors are easily generated.
in this case, we can consider a variety of optimization methods, such as the following chain-Optimized .
( PHP Development,web front-end,UI design,VR Development Professional training institutions --v IT College Copyright, reproduced Please indicate the source, thank you for your cooperation! )
Js Functional Programming concept (knowledge sharing at V-College)