Anonymous functions

Self-executing function

Functional expression

The callback function

Anonymous function nomenclature Features Anonymous function nomenclature features

1. Set a name that is not declared in its context

(function A() { // ... }) (); console.log(A); //Uncaught ReferenceError: A is not definedCopy the code

2. In the private context of function execution, the name is “stored in the AO” as a private variable whose value is the “heap memory address” of the current function itself; And by default, changing the value of this variable is invalid;

(function A() { A = 100; // Invalid console.log(A); // function itself})();Copy the code

3. Any function inside, the name of our manual stated “parameter, for example, / var/function/let/const…” The default value of this variable is the current function itself. “Default is this point in the function itself, the weight is too low.”

/ EC (AA) * * * A -- -- -- -- -- -- -- -- -- > function itself * scope chain ascension: - * * parameter assignment variables var A * / (function A () {the console. The log (A); //undefined var A = 100; console.log(A); / / 100}) (); EC (AA) / * * * A -- -- -- -- -- -- -- -- -- > function itself * * parameter scope chain assignment A = = = 100 * variables: - * / (function A (A) {the console. The log (A); //100 A = 200; console.log(A); / / 200}) (100); (function A(A) { console.log(A); //100 function A(){} console.log(A); / / 200}) (100); / * /! * * EC(G) * b --> 10 * * *! / var b = 10; (function b() { /! EC * * * B (B) private context - > the current function is * scope chain: < EC (B), EC (G) > * parameter assignment: - * variables: - *! / b = 20; // Invalid console.log(b); // The current function itself})(); console.log(b); / / 10Copy the code

Anonymous function nomenclature:

Because we can access this function inside of the function with this name, we can implement some capabilities that we don’t have otherwise, such as recursive “use strict”; Open JS strict mode “syntax to be more rigorous, currently we develop, based on Webpack packaging, are strict mode”

"use strict"; (function () {// recursive? // arguments.callee: specifies the current function. // arguments.callee. Caller: specifies where the storage function is executed console.log(arguments); }) (); (function A() {// console.log(A); // the function itself A(); }) (); // Dead recursive memory overflowCopy the code

The arguments. The arguments and the callee. The callee. Caller

  • Arguments. callee: Refers to the function itself
  • Arguments.callee. caller refers to the host environment in which the function is executed. If it is executed in function A, it prints A; if it is executed in global scope, it prints null.
function fn(){ console.log(arguments.callee); } fn(); function fn(){ console.log(arguments.callee.caller); } fn(); Function fn(){console.log(arguments.callee.caller); function fn(){console.log(arguments.callee.caller); } function A(){ fn(); } A();Copy the code