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.

Higher-Order Functions Fundamentals的更多相关文章

  1. [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: Higher Order Functions

    [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: ...

  2. 46 Simple Python Exercises-Higher order functions and list comprehensions

    26. Using the higher order function reduce(), write a function max_in_list() that takes a list of nu ...

  3. [React] Higher Order Components (replaces Mixins)

    Higher order components will allow you to apply behaviors to multiple React components. So the idea ...

  4. [React] Implement a Higher Order Component with Render Props

    When making a reusable component, you'll find that people often like to have the API they're most fa ...

  5. [Redux] Understand Redux Higher Order Reducers

    Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a ...

  6. [React Intl] Use a react-intl Higher Order Component to format messages

    In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...

  7. [React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component

    In this lesson we'll create a Higher Order Component (HOC) that takes care of the key property that ...

  8. [RxJS] Flatten a higher order observable with concatAll in RxJS

    Besides switch and mergeAll, RxJS also provides concatAll as a flattening operator. In this lesson w ...

  9. [RxJS] Flatten a higher order observable with mergeAll in RxJS

    Among RxJS flattening operators, switch is the most commonly used operator. However, it is important ...

随机推荐

  1. OpenStack 删除instance 和其附加的volumes

    在openstack里面有时候删除instance时,volume无法跟着删除,可以自己编写脚本来实现, 脚本代码如下: #!/bin/bash for i in $(cat /root/host-d ...

  2. 数据开源工具:Hadoop为企业带来什么?

    熟悉大数据的人一定不会对大名鼎鼎的Hadoop工具陌生,Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.Hadoop的框架最核 ...

  3. idea 将项目托管到 Git 报错:Can't finish Gitee sharing process

    在idea中报: Can't finish Gitee sharing processSuccssully created project 'dmp' on Gitee. but initial co ...

  4. Jenkins如何进行权限管理

    一.安装插件 插件名:Role-based Authorization Strategy 二.配置授权策略 三.创建用户 四.添加并配置权限 4.1.添加Global Role 普通角色拥有全局只读权 ...

  5. P5343 【XR-1】分块(dp矩阵加速)

    \(大意是用数组a里的数字,组成一个序列,使得序列和为n的方案种数\)传送门 \(先考虑dp.\) \(但是不能直接用背包转移,因为是序列,要考虑顺序.\) \(所以,为了去重,我们令dp[i][j] ...

  6. 线段树 离散化 E. Infinite Inversions E. Physical Education Lessons

    题目一:E. Infinite Inversions 这个题目没什么思维量,还比较简单,就是离散化要加上每一个值的后面一个值,然后每一个值放进去的不是1 ,而是这个值与下一个点的差值. 因为这个数代表 ...

  7. Day_09【常用API】扩展案例2_测试小字符串在大字符串中出现的次数

    分析以下需求,并用代码实现 1.键盘录入一个大字符串,再录入一个小字符串 2.统计小字符串在大字符串中出现的次数 3.代码运行打印格式: 请输入大字符串: woaiheima,heimabutongy ...

  8. 把99%的程序员烤得外焦里嫩的JavaScript面试题

    最近有学员给出一段令人匪夷所思的JavaScript代码(据说是某某大厂面试题),废话少说,上代码:   var a = 10; { a = 99; function a() { } a = 30; ...

  9. 拥抱K8S,Spring Boot 2.3提供K8s活性和就绪性探针

    Spring Boot 2.3将包括对Kubernetes liveness&readiness Probe的本地支持,扩展了2.2的"运行状况组"支持,允许开发人员选择运 ...

  10. GIT代码版本管理

    一.实验目的 1.了解分布式版本控制系统的核心机理: 2.熟练掌握git的基本指令和分支管理指令: 二.实验内容 1.安装git: 2.初始配置git,git init git status指令: 3 ...