【英文】JS高阶函数

Preface

Notes on learning advanced functions in JS

If a function takes a function as a parameter (such as a callback function), then this function is a higher-order function. If a function’s return value is a function (such as a closure), then this function is a higher-order function.

Callback Function

1
2
3
4
5
6
7
function fn(callback) {
callback && callback();
}

fn(function() {
...
});

Closure

  • A function that can access the local variables of another function is called a closure.
  • The main purpose of a closure is to extend the scope of local variables.
1
2
3
4
5
6
7
8
9
function father() {
var variableName = value;
function son() { // closure
variableName = value;
};
return son;
}

var son = father();

Small Closure

  • Immediately invoked function expressions (IIFE) are called small closures because the parameters passed to the IIFE can be used inside the IIFE.
1
2
3
(function(i) {
console.log(i);
})(i);

Completion

References

Bilibili - HEIMA Front-End