zoukankan      html  css  js  c++  java
  • Higher-Order Functions Fundamentals

    Higher-Order Functions

    A function that accepts and/or returns another function is called a higher-order function.

    It's higher-order because instead of strings, numbers, or booleans, it goes higher to operate on functions. Pretty meta.

    With functions in JavaScript, you can

    Store them as variables

    Use them in arrays

    Assign them as object properties(methods)

    Pass them as arguments

    Return them from other functions

    Functions Operate on Data

    Strings Are Data

    sayHi = (name) => `Hi, ${name}!`;
    result = sayHi('Unity');
    
    console.log(result);	// Hi, Unity!
    

    Numbers Are Data

    double = (x) = > x * 2;
    result = double(4);
    
    console.log(result);	// 8
    

    Booleans Are Data

    getClearance = (allowed) => (allowed ? 'Access granted' : 'Access denied');
    
    result1 = getClearance(true);
    result2 = getClearance(false);
    
    console.log(result1);	// Access granted
    console.log(result2);	// Access denied
    

    Objects Are Data

    getFirstName = (obj) => obj.firstName;
    
    result = getFirstName({
       firstName: 'Bjarne' 
    });
    
    console.log(result);	// 'Bjarne'
    

    Arrays Are Data

    len = (array) => array.length;
    result = len([1, 2, 3]);
    
    console.log(result);	// 3
    

    What makes them first-class? You can pass them around, store them in variables and arrays, use them as inputs for calculations. You can use them like any piece of data.

  • 相关阅读:
    面试题整理
    Node.js核心模块-stream流
    Node.js核心模块-crypto加密
    Node.js核心模块-assert
    Node.js全局对象-process
    nodemon
    随笔
    python学习笔记(十四): unittest
    python学习笔记(十三): 多线程多进程
    python学习笔记(十二):发送邮件
  • 原文地址:https://www.cnblogs.com/PrimerPlus/p/12786206.html
Copyright © 2011-2022 走看看