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.
Higher-Order Functions Fundamentals的更多相关文章
- [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: ...
- 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 ...
- [React] Higher Order Components (replaces Mixins)
Higher order components will allow you to apply behaviors to multiple React components. So the idea ...
- [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 ...
- [Redux] Understand Redux Higher Order Reducers
Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 2019-05-22 BTC走势分析
转载于:https://juejin.im/post/5ce4adf5f265da1b8c195ff8
- 快速上手最新的 Vue CLI 3
翻译:疯狂的技术宅 原文:blog.logrocket.com/getting-sta- 概要:本文将指导你快速上手 Vue CLI 3,包括最新的用户图形界面和即时原型制作功能的使用步骤. 介绍 尤 ...
- 获取系统DPI、系统显示比例等
using System; using System.Drawing; using System.Runtime.InteropServices; namespace XYDES { public c ...
- Java反射与注解
反射 能够分析类能力的程序称为反射(reflective),代码的这种能力称为"自省".反射机制的功能极其强大,反射机制可以用来: 在运行时分析类的能力 在运行时查看对象,例如,编 ...
- Codeforce 1098-A
A. Sum in the tree Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root ...
- Effective C++学习记录
Effective C++算是看完了,但是并没有完全理解,也做不到记住所有,在此记录下55个条款及条款末的"请记住". 让自己习惯C++ 条款01:视C++为一个语言联邦 ① C ...
- 题目分享E 二代目
题意:一棵点数为n的树,每个节点有点权,要求在树中中找到一个最小的x,使得存在一个点满足max(该点点权,该点相邻的点的点权+1,其他点的点权+2)=x 分析:首先要能把题目转化为上述题意 首先题目让 ...
- 使用Hystrix的插件机制,解决在使用线程隔离时,threadlocal的传递问题
背景 在我们的项目中,比较广泛地使用了ThreadLocal,比如,在filter层,根据token,取到用户信息后,就会放到一个ThreadLocal变量中:在后续的业务处理中,就会直接从当前线程, ...
- leetcode_二叉树验证(BFS、哈希集合)
题目描述: 二叉树上有 n 个节点,按从 0 到 n - 1 编号,其中节点 i 的两个子节点分别是 leftChild[i] 和 rightChild[i]. 只有 所有 节点能够形成且 只 形成 ...
- ActiveMQ 事务、集群、持久订阅者、ActiveMQ监控
JMS介绍 JMS是什么? JMS的全称Java Message Service,既Java消息服务. JMS是SUN提供的旨在统一各种MOM(Message-Oriented Middleware) ...