JavaScript series: Functional Programming (opening)

Source: Internet
Author: User

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.
  1. To function as an abstract unit
      • For example, we want to customize the printing error, we can abstract the concept of error, warning, etc. into a function
        1 function fail (err) {2thrownew  Error (err); 3 }4function  warn (err) {5 console.log ("Warning thing:%s", err); 6 }   

  2. 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 instance
      1 Person1.school 2 // = undefined

      The second example can be accessed and can be Modified.
  3. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.