Introduction to higher-order functions in JavaScript

Source: Internet
Author: User

Higher order functions: higher order looks like an esoteric term for advanced programming techniques, which I think I did when I first saw it.

Higher-order functions of JavaScript

However, a higher-order function is simply a function that takes a function as a parameter or return value . Take the following hello,world as a simple example.

var Moqi = function (p1) {    this. Add = function (p2) {        return' + p2;    };     return add;}; // We can use this function Console.log (Moqi ('Hello') (' World  '));

Maybe the process is a bit confusing, just look at the details.

typeof Moqi ('hello')//"function"Moqi ('Hello ' )//function (p2) {//    return p1 + "+ p2; // }

In other words, Moqi (' Hello ') is a function, Moqi (' Hello ')

var m = Moqi ('Hello')       m ('World')  //"Hello,world"

From the above, higher-order functions can make the code more concise and efficient. Naturally, we can also create a function like this:

varMoqi =function (p1) {returnfunction (p2) {returnfunction (p3) {returnP1 +','+ P2 +' '+P3; }    };}; Moqi ('Hello')(' World')('Phodal')//"Hello,world phodal"

Restore higher-order functions

Increasingly complex, the need to introduce higher-order function abstractions is the occurrence of repetitive or similar code. We then revert to the previous function:

var Moqi = function (p1) {     this. Add =  function (p2) {        return  function ( P3) {            return', ' ' +p3;        }    };     return  This . Add;};

And then create a new function.

 var  moqi =        Function (p1) { this . Add = function (p2) {  this . Add1 = function (p3) { 
    
     return 
     p1 +  " ,   " + P2 + "          '  +

Using the call method in JavaScript, you will have:

varMoqi =function (p1) {varSelf = This; function fd (p2) { This. Add1 =function (p3) {returnP1 +','+ P2 +' '+P3;    }; } Self.add=function (p2) {Fd.call ( This, p2); return  This. Add1;    }; returnSelf.add;};

Examples of higher order functions

Add = function (b) {    return a + b;}; function Math (func,array) {    return func (array[0],array[1]);} Console.log (Math (add,[1,2])); // Math (add,[1,2]) // 3

Introduction to higher-order functions in JavaScript

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.