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 ...
随机推荐
- 【Linux常见命令】ifconfig命令:配置与查看网络信息
ifconfig(interfaces config).通常需要以root身份登录或使用sudo来使用ifconfig工具 ifconfig 命令用来查看和配置网络设备.当网络环境发生改变时可通过此命 ...
- 我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色
通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...
- 求x>0时,y=x^3-6x^2+15的极值
解: 当x→∞时,y也→∞,所以y没有最大值. y=x3-6x2+15=-4*(x/2)*(x/2)*(6-x)+15 而根据几何平均数小于等于算术平均数的定理,(x/2)*(x/2)*(6-x)在x ...
- ACM--[kuangbin带你飞]--专题1-23
专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...
- ASP.NET MVC使用log4net
本文介绍如何在ASP.NET MVC中使用log4net来记录日志文件. 参考链接:https://www.codeproject.com/Articles/823247/How-to-use-Apa ...
- Redis系列(六):设置/移除键的过期时间
本篇博客是Redis系列的第6篇,主要讲解以下内容: 数据库数量 切换目标数据库 设置键的过期时间 移除键的过期时间 本系列的前5篇可以点击以下链接查看: Redis系列(一):Redis简介及环境安 ...
- 无回显命令执行TIPS
DNSlog 出属于, POST DATA HEX 等一些 命令延迟注入 找WEB PATH 将id,pwd,hostname的结果写在js/test1.txt中,命令find . -type ...
- JavaScript 循环判断练习题
JavaScript 循环判断练习题 小明有一组水果("苹果","梨子","香蕉","葡萄","西瓜" ...
- 一篇博客带你轻松应对java面试中的多线程与高并发
1. Java线程的创建方式 (1)继承thread类 thread类本质是实现了runnable接口的一个实例,代表线程的一个实例.启动线程的方式start方法.start是一个本地方法,执行后,执 ...
- SSM + MYSQL 酒店客房管理系统
酒店客房管理系统的设计与实现是采用JSP技术,MYSQL数据库进行开发的.系统具有灵活的一体化设计方式,圆满完成了整个系统的全面设计,系统主要采用JSP技术开发,提高系统的运行性能和安全性,并且易于维 ...