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. 【Linux常见命令】tee命令

    tee - read from standard input and write to standard output and files tee命令用于读取标准输入的数据,并将其内容输出成文件. t ...

  2. Android Room SQLite持久层框架

    原文链接 前言 Android中提供了SQLite数据库进行数据的持久化 ,并提供了对应API访问数据库,而Room框架提供了SQLite数据访问抽象层,为高效的数据库访问层带来便捷 APP可以缓存用 ...

  3. 补题Codeforces 1102E. Monotonic Renumeration

    这个题还是不太懂,下面附上的是大佬的题解(https://zhanghuimeng.github.io/post/codeforces-1102e-monotonic-renumeration/) E ...

  4. Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) A Dead Pixel

    讨论坏点的左右上下的矩形大小. #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> ...

  5. POJ 1176 Party Lamps&& USACO 2.2 派对灯(搜索)

    题目地址 http://poj.org/problem?id=1176 题目描述 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码. 这些灯都 ...

  6. centos下的redis一键安装shell脚本

    #!/bin/bash yum install cpp binutils glibc-kernheaders glibc-common glibc-devel gcc make wget #安装依赖库 ...

  7. webpack----js的静态模块打包器

    webpack----js的静态模块打包器 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 简介 webpack 是一个现代 J ...

  8. spring内嵌jetty容器,实现main方法启动web项目

    Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可以将 ...

  9. 这么多Linux版本,你究竟该怎么选择?

    Linux有非常多的版本,比如世面上常见的有 Ubuntu.RedHat.Fedora.Centos等等,这么多的版本我们究竟该选哪一个呢?今天我带大家对各个版本进行一下分析和比较,帮助大家来做出更好 ...

  10. QML设计飘散效果

    1,目标及展示 首先希望实现文字.图片.控件等在触发后,呈现飘散并消失的效果.在QT例程<Qt Quick Particles Examples>是一个海星点击鼠标后呈现打散的效果,这个效 ...