This is the 7th day of my participation in the August More Text Challenge

Function function, reduce repeated code, code decoupling. Isolate a function point. Increases code reading

Create (define, declare) functions

Function name (){}Copy the code

The function itself does not run, only if the function is called. The way to call a function is also simple: call the function name () directly

The characteristics of

  1. Functions declared by literals,Will be promoted to the top of the script, and define traversalvarThe same.

  2. Functions declared by literals become global properties. .
  3. Using the typeof function name, the result is'function'

  4. Function internal code block that declares variables inEs6 did not use var beforeThe declared variable is placed globally when the function is executedwindowThe above.Es6 Strict modeCannot be defined, an error will be reported.

  5. If you use the inside of the functionvarTo declare variables,The current variable will not be promoted to the windowI’m going to go to the delta functionThe top of the code blockIn theExternal is unable to use this variable. Means functions can have code scope.

parameter

If a function has no parameters, it means nothing

Function Function name (parameter 1, parameter 2...) {}Copy the code

Parameters and arguments correspond one to one. If there are too many arguments, the function does not care. If there are too few arguments, the missing part will be filled with undefined

The return value

After the function is run, the result of calling the function is the return value. Any value that is not returned is returned by defaultundefined. A return prevents the code from running, and subsequent code will not be run.

Documentation comments

Functions are very common in code, but the meaning of each function is unknown to many people, so there is a kind of documentation comment to comment functions or variables



Function scope

In JS, there are three types of scope:

  1. Global scope: used externally in early JSvarDeclared variables are promoted to the top of the script block and become properties of the global object
  2. Function scope: Used in function scopevarDeclared variables are promoted to the top of the function and do not become properties of the global object. Therefore, variables declared in functions do not pollute global objects.
  3. Block-level scope: this will pollute global variables because of the global scope. ines6After, useletorconstTo define variables, one particular function here is block-level scope, where variables can only be declared in one{}In effect.

Therefore, variables declared in functions do not pollute global objects, and try to encapsulate functionality in functions

However, when a function becomes an expression, it neither promotes nor pollutes the global object. What are some ways to turn a function into a function expression?

  1. Use the functionparenthesesPut them in brackets, and you get a function expression



    We know that the expression will return a value,(function test1 () {})It returns a function, so the way you call it is, you add a function(a)This is calling a function.

In this case, if you write a Function expression and then execute it Immediately, the Function is called IIFE(Immediately Invoked Function Expresion). Since, in most cases, a function that executes immediately cannot be called by later code, the function name can be omitted. A function that omits its name is called an anonymous function

Variables that can be used in a scope

A global scope can use only variables declared in the global scope (including functions). A function scope can use not only variables or functions in the function scope, but also variables or functions in the global scope.

The above two sentences mean that the global scope is greater than the function scope

Outer scopes cannot use inner scopes; inner scopes can use outer scopes.

var a = '23';
function A (){
	console.log(a)
	B();
    console.log(b);
	function B(){
	 var b = 'b';
	}
}

A();
Copy the code

closure

A closure is a phenomenon in which an inner function uses a variable or function in an outer function environment.

var test = '2323';
function A(){
	console.log(test)
}


Copy the code

Detailed closure, later slowly chat