Objective:
The previous article introduced function callbacks, Higher-order functions, and function currying, as well as advanced function applications, and because we are learning JavaScript functional programming, want to tidy up functional programming, for our daily more useful parts.
Why is functional programming important?
Having studied C++,java these object-oriented programming languages, we probably all know that object-oriented programming is to divide the target problem into several parts, realize the function of each part, and combine it into a more powerful object. While JavaScript is not an object-oriented programming language (although ES6 already has a class object like this, this is not the case), it is a language that completely supports functional programming, and with functional programming, we can also break the problem down into several parts (functions) to Solve. Functional programming also constructs more powerful functions by combining other functions to achieve more abstract behavior.
- To function as an abstract unit
- Encapsulating and hiding
- JavaScript does not provide a way to hide data directly (that is, There is no private variable), but you can use closures to implement hidden data (the definition of a private variable)
1 varperson=function(){2 //Defining private Variables3 varSchool= "scut";4 //defining public interface methods5 return {6Name: ",7SetName:function(name) {8 this. name=name;9 },TenGetName:function(){ one return this. name; a }, -Getschool:function(){ - returnschool; the } - } - } - varperson1=Newperson () + //= undefined - Person1.school + //= undefined a Person1.getschool () at //= "scut"
1 varperson=function(){2 this. Name= ';3 this. school= "scut";4 this. setname=function(name) {5 this. name=name;6 };7 this. getname=function(){8 return this. name;9 };Ten this. getschool=function(){ one return this. school; a } - } - varperson1=Newperson () the Person1.getschool () - //= "scut" - Person1.school - //= "scut" +Person1.school= ' Jyyz ' - Person1.getschool () + //= "jyyz"
By the first example we can see that school is equivalent to a private variable and cannot be accessed through an instance1 Person1.school 2 // = undefined
The second example can be accessed and can be Modified.
- Act as a function unit
- A discrete unit that provides a simple storage and transfer behavior
- To give a simple example: array indexing behavior, called nth, is simple as follows
1Nativenth (' Hello ', 2)2 //= "l"3Nativenth ({},2)4 //= undefined5 as you can see, an error occurs when an incorrect value is passed in, and is modified as Follows:6 functionnativenth (a,index) {7 if(! Number.isinteger (index)) fail ("expected A number as the index");8 if(! Array.isarray (a) &&typeofa!== ' String ')9Fail ("Non supported on non-indexed type");Ten if((index<0) | | (index>a.length-1)) oneFail ("index value is out of bounds"); a returna[index]; -}
In this way, we construct the abstract way of the nth Function. summary: generally speaking, functional programming consists of the following points:identify abstractions and build functions for themUse existing functions to build more complex abstractionsto construct more complex abstractions by passing existing functions to other functionsthrough the function, we can encapsulate each part function in the abstraction, provide an interface, realize the independence of LOGIC.
JavaScript series: Functional Programming (opening)