Today we’re going to focus on functions in JavaScript. Functions are very important in JavaScript, and there are many different ways to use them. Today we’re going to review arrow functions, higher-order functions, Coriolization functions, and pure functions. What does it do in JS?

Today’s topic:

  • What is a lambda or arrow function? What’s the difference?
  • What is a first-class citizen in JS?
  • What is a higher-order function?
  • What is a Currie function?
  • What is a pure function?

1. What is a lambda or arrow function? What’s the difference?

Arrow functions are a feature of ES6. Arrow functions are short syntax for function expressions. They don’t have their own this, arguments, and can’t be used as constructors. Use normal functions if you need them.

  • There is no this
const obj = {
    a: 1.getA: () = > {
        console.log(this.a)
    },
  	getB: function() {
      	console.log(this.a)
    }
}
obj.getA() // undefined
obj.getB() / / 1
Copy the code
  • Without the arguments
const fn = () = > console.log(arguments)
// Uncaught ReferenceError: arguments is not defined
Copy the code
  • Cannot be used as a constructor
const Foo = () = > {};
const foo = new Foo(); // Uncaught TypeError: Foo is not a constructor
Copy the code
  • No Prototype attribute
const Foo = () = > {};
console.log(Foo.prototype); // undefined
Copy the code

2. What is a first-class citizen in JS?

In the JavaScript language, functions are first-class citizens. First-class citizenship means that functions can be used anywhere in JavaScript as variables.

For example, in JS, a function can be passed as an argument to and returned by other functions, or it can be assigned as a value to a variable.

In the following example, the handler function is passed as a variable to the addEventListener function.

const handler = () = > console.log ('This is an event listener.');
document.addEventListener ('click', handler);
Copy the code

3. What are higher-order functions?

In simple terms, higher-order functions are functions that take a function as an argument or return a function as a return value, or both.

const log = () = > console.log ('hello world');
const callFun = fn= > fn();
callFun(log);
Copy the code

4. What is a Currie function?

Currization is the process of converting functions with multiple parameters into a sequence of functions with only one parameter each. Currying is named after the mathematician Haskell Curry.

const add = (a, b, c) = > a + b + c;
console.log(add(1.2.3)); / / 6

const curryUnaryFunction = a= > b= > c= > a + b + c;
curryUnaryFunction (1); // Output: b => C => 1 + b + c
curryUnaryFunction (1) (2); // Output: c => 3 + c
curryUnaryFunction (1) (2) (3); // Output: 6
Copy the code

The Currization function may not be familiar or even heard of, but it is very suitable for increasing the reusability of code and achieving higher order encapsulation of function composition.

What is a pure function?

A pure function is one whose return value is determined only by its arguments, without any side effects. That is, if the same argument is entered at any time, the function returns the same value.

For example, the difference between a pure function and an impure function

// an impure function
let numberArray = [];
const impureAddNumber = number= > numberArray.push(number);

/ / pure functions
const pureAddNumber = number= > argNumberArray= >
  argNumberArray.concat([number]);

console.log (impureAddNumber(6)); // Output: 1
console.log (numberArray); // Output: [6]
console.log (pureAddNumber(7)(numberArray)); // Output: [6, 7]
console.log (numberArray); // Output: [6]
Copy the code

The first function changes the array outside the function and returns a value independent of the argument, so this is not a pure function.

Another function uses concat to merge the array passed in with another array, producing a brand new array with no side effects.

Pure functions are important because they make unit testing easier, have no side effects, do not require dependency injection and avoid tight coupling.

nagging

Today’s cover is a little bit more in-depth, but it’s essential to be a good front end engineer.

Thank you for reading, your likes, comments and concerns are my biggest encouragement O(∩_∩)O👍👍👍

I am aufu Kosi, pay attention to me, let us progress a little bit every day, look forward to a better themselves.