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. HTML JavaScript 基础(上)

    一.初识JavaScript JavaScript 和 Java什么关系? 半毛线关系都没有,只是名字有点重合而已. JavaScript 和python.C#.Java.Ruby一样,都是一门独立的 ...

  2. CentOS配置Tomcat监听80端口,虚拟主机

    2019独角兽企业重金招聘Python工程师标准>>> Tomcat更改默认端口为80 更改的配置文件是: /usr/local/tomcat/conf/server.xml [ro ...

  3. 02-线性结构3 Reversing Linked List

    02-线性结构3 Reversing Linked List   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...

  4. 导入sql错误

    2019独角兽企业重金招聘Python工程师标准>>> 导入sql错误: This function has none of DETERMINISTIC, NO SQL, or RE ...

  5. CentOS上安装比较习惯的代码编辑器

    linux下的vim用起来不是很习惯,可能是能力有限.所以一直在找一种自己比较熟悉的代码编辑器,所以就找到了sublime text,安装方法网上有很多种,比较方便的方法:直接在csdn上下载一个破解 ...

  6. 商汤提出解偶检测中分类和定位分支的新方法TSD,COCO 51.2mAP | CVPR 2020

    目前很多研究表明目标检测中的分类分支和定位分支存在较大的偏差,论文从sibling head改造入手,跳出常规的优化方向,提出TSD方法解决混合任务带来的内在冲突,从主干的proposal中学习不同的 ...

  7. sequel pro无法连接mysql服务器

    1. 添加用户 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_passwd' WITH GRANT OPTION; FLU ...

  8. EOS基础全家桶(十)交易Action操作

    简介 区块链上的所有操作都是通过交易(Transaction)上链的,无论你是转账交易还是发起的智能合约的调用,而EOS和传统区块链不同的是EOS在一个交易里可以发起多个行为(Action),这使得E ...

  9. (Lineup the Dominoes筛子)三维状压

    传送门 描述:\(一堆筛子,每个筛子两个面,上面有1-6之间的数字.后一个筛子与前一个筛子的接触面的点数必须相等.\) \(求,有多少种方案堆完筛子.(方案只关心筛子的位置,不关心是否翻转)\) \( ...

  10. D - Silver Cow Party J - Invitation Cards 最短路

    http://poj.org/problem?id=3268 题目思路: 直接进行暴力,就是先求出举行party的地方到每一个地方的最短路,然后再求以每一个点为源点跑的最短路. 还有一种方法会快很多, ...