Why does JavaScript have

First-class function?

Peter Chen

2018-01-27

First-class function
is a necessity
for the functional programming

Agenda

  • First-class citizen
  • First-class function

First-class citizen

Definition

An entity which supports all the operations generally available to other entities.

Operations

  • Pass as an argument
  • Returned from a function
  • Modified
  • Assigned to a variable

Question

Who is First-class citizen?

Integer In C is an First-class citizen

#include <stdio.h>

// Pass as an argument
int integer_is_first_class_citizen ( int arg ) {
    return arg; // Returned from a function
}

int main()
{
    int arg = 0;    // Assigned to a variable
    
    arg = 1;    // Modified
    
    integer_is_first_class_citizen ( arg );
    
    printf("Integer is a First-class citizen\n");

    return 0;
}

Array In C isn't an First-class citizen

#include<stdio.h>

// arr is a pointer to the array's first element
void array_is_not_first_class_citizen ( int *arr, int count ) {
    for(int i = 0; i < count; i++) {
        arr[i]++;
    }
}

int main() {
    int arr[] = {0, 1, 2, 3, 4};
   
    array_is_not_first_class_citizen ( arr, 5 );
    
    for(int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }   // 1 2 3 4 5
}

First-class function

Definition

Treat function as First-class citizen

Feature

  • Higher-order functions
  • Nested functions
  • Anonymous functions
  • Non-local variables

Higher-order function

  • Take one or more functions as arguments
  • Returns a function as its result
//  Higher-order function
//  1. Take one or more functions as arguments

function takeFuncAsArg(callback) {
  return callback("Arguments");
}

function helloWorld(featureName) {
  return "Hello " + featureName;
}

console.log(takeFuncAsArg(helloWorld));

//  2. Returns a function as its result

function returnFunc(callback) {
  return callback;
}

console.log(returnFunc(helloWorld)("Results"));

Nested function

A function which is defined within another function

// 2. Nested function
// A function which is defined within another function

function outterFunc(){
  function innerFunc() {
    return 'Inner';
  }
  
  return 'Hello Outter ' + innerFunc();
}

console.log(outterFunc());

Anonymous function

A function definition that is not bound to an identifier

// 3. Anonymous function
// A function definition that is not bound to an identifier

console.log(function() {return 'Hello Anonymous'}());

Non-local variable

A variable that is not defined in the local scope

// 4. Non-local variable
// A variable that is not defined in the local scope

function nonLocalVariable() {
  // outter is non-local variable for innerFunc
  var outter = "Non-local variable";

  function innerFunc() {
    return outter;
  }

  return "Hello " + innerFunc();
}

console.log(nonLocalVariable());

Reference

Sample Code

Thank You

Why does JavaScript have First-class function?

By Peter Chen

Why does JavaScript have First-class function?

Explain First-class function in JavaScript

  • 438