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. JavaScript HTMlL DOM对象(上)

    Dom:document.相当于把所有的html文件,转换成了文档对象. 之前说过:html-裸体的人:css-穿上衣服:js-让人动起来. 让人动起来,就得先找到他,再修改它内容或属性. 找到标签 ...

  2. Bootstrap表格组件 Bootstrap Table

    Bootstrap Table是Bootstrap的一个组件 Bootstrap Table Demo:http://issues.wenzhixin.net.cn/bootstrap-table/i ...

  3. Django项目打包

    Django项目打包 这是目前开发完成的project目录树.我们要打包其中的polls app. (v_python3.6) thinkt@linux-pw37:~/PycharmProjects/ ...

  4. iterm2终端manpage高亮显示

    iterm2 据说是macOS平台上最好用的终端工具,今天下载来用了一下,发现确实很好.但是在使用man命令显示manpage的时候,颜色配置不是很让人满意,就像下面这样: 其实这样也是不错的,但是用 ...

  5. 《SQL初学者指南》——第1章 关系型数据库和SQL

    第1章 关系型数据库和SQL SQL初学者指南在本章中,我们将介绍一些背景知识,以便于你能够很快地上手,能在后续的章节中编写SQL语句.本章有两个主题.首先是对本书所涉及到的数据库做一个概述,并且介绍 ...

  6. mysql查询语句中like 的用法

    1.常见用法: (1)搭配%使用 %代表一个或多个字符的通配符,譬如查询字段name中以大开头的数据: (2)搭配_使用 _代表仅仅一个字符的通配符,把上面那条查询语句中的%改为_,会发现只能查询出一 ...

  7. Spring 注解注入—@Qualifier 注释

    当创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean ...

  8. Mariadb 主主复制

    两台server1  192.168.1.189 &&  server2 192.168.1.226 安装mariadb数据库 yum -y install mariadb maria ...

  9. Fibonacci相关问题

    公式如下: 递归的解法我就不写了,贴一个递推的. long long Fibonacci(unsigned int n) { ) ; ) ; ; ; long long res; ; i <= ...

  10. C++中const的特性

    目录(作用): 1:修饰变量,说明该变量不可以被改变: 2:修饰指针,分为只想常量的指针和自身是常量的指针 3:修饰引用,指向常量的引用,用于修饰形参,即避免了拷贝,有避免了函数对值的修改: 4:修改 ...