function
A function is a function that encapsulates some statements inside a function and has a specific function
Function name () {}
Call: function name ();
Basis function
1. Declaration and invocation of functions
Function declaration: Defines functions
Key word: function
Grammar:
Function Name (parameter) {structure}
Call:
The function name ();
note
- Function names must follow identifier definition rules
- Functions must be declared before they can be called
- When a function is declared, the internal statement is not executed
- The location of the function execution is independent of the declared location, but depends on the location of the call
- Function call syntax: function name (argument);
- The function can be called multiple times
function fun() {
console.log(10);
}
console.log(1);
fun();
/ / 1
/ / 10
Copy the code
2. Parameters of a function
The function can help us encapsulate some code, the code can be repeated through the call, the function left an interface, is our parameter, through the change of parameters to make our function have different functions.
Parameters are variables: they are named the same way variables are
Parameters: When a function is defined, the parameters in parentheses are called formal parameters
Arguments: When a function is called, the arguments in parentheses are called actual arguments
Parameter passing: The process of passing actual parameters to formal parameters when a function is executed
JavaScript is a dynamically typed data language, where the type of a variable changes depending on what is stored in it. The data types of the arguments affect the data types of the parameters, causing the output structure of the function to change.
function sum(a, b) {
console.log(a + b);
}
sum(1.2);
sum(1.'2');
sum(5); // 5 + undefined
// 12 number
// 12 string
// NaN
Copy the code
3. Function return valuereturn
A function can use a return statement to take arguments and operate on them. A return simply returns a value, not output.
If the return keyword is executed in a function, the execution stops immediately and the function returns directly
function sum(a, b) {
return a + b;
console.log(5); / / does not perform
}
console.log(sum(1.2));
/ / 3
Copy the code
4. Modular programming cases
Case 1: Determine a prime number up to 1000
=== === === === =
function YSGS(a) {
var sum = 0;
for (var i = 0; i <= a; i++) {
if (a % i === 0) { sum ++; }}return sum;
}
function isZS(b) {
return YSGS(b) === 2 ? true : false;
}
for (let i = 2; i < 1000; i++) {
let msg = isZS(i) === true ? It's prime. : It's not prime.
console.log(i + msg);
}
Copy the code
Perfect numbers in cases 2:1 to 1000
The sum is equal to itself
function YSH(a) {
let sum = 0;
for (let i = 1; i < a; i++) {
if (a % i === 0) { sum += i; }}return sum;
}
function isWMS(a) {
return YSH(a) === a ? true : false;
}
function WMS(total) {
for (let i = 2; i < total; i++) {
if (isWMS(i)) {
console.log(i + It's a perfect number.);
}
}
}
WMS(10000);
// 6 is a perfect number
// 28 is perfect
// 496 is perfect
// 8128 is a perfect number
Copy the code
5. Functional expressions
Function definitions can be declared using the keyword function, or functions can be defined using variables, called anonymous functions
var sum = function(a, b) {
return a + b;
};
console.log(sum(1.2));
/ / 3
Copy the code
6. The data type of the function
-
Simple data types
var a = 3; var b = a; a = 4; console.log(a); console.log(b); / / 4 / / 3 Copy the code
-
Reference data type
Reference data types pass addresses that interact with each other for details
var fun1 = function () { console.log(1); } var fun2 = fun1 fun2.haha = 10; console.log(fun1.haha); / / 10 Copy the code
7. Enhancement of function declarations
-
Variable declarations must be promoted: reference first, then definition, only definition, not assignment
-
Function declaration must be promoted: call first, then declare, only promote the function life, not promote the definition, function name points to the address of the function, so when calling a function, the statement inside the function will also be executed
According to this feature, we usually call the function first and declare the function last.
fun();
function fun() {
console.log(10);
}
/ / 10
Copy the code
When a function and a variable have the same name
If the variable has a value, then the output is the value of the variable.
If the variable has no value, the output is this function.
var sum = 5;
function sum() {
console.log(3);
}
console.log(sum);
var num;
function num() {
console.log(3);
}
console.log(num);
console.log(a);
var a = 5;
function a() {
console.log(3);
}
/ / 5
// [Function: num]
// [Function: a]
Copy the code
When the keyword declaration has the same name as the function expression, the keyword declaration overrides the function expression
foo();
function foo() {
console.log(5);
}
var foo = function () {
console.log(3)}/ / 5
Copy the code
Function is advanced
1. Recursive functions
define
To call a function by name from within a function
A function can call itself, which is called recursion
application
Recursive functions are often used to deal with mathematical problems
Example: Fibonacci sequence: 1 1 2 3 5 8 13 21 34
// Use a function to find any term in the Fibonacci sequence
function feibo(n) {
for (let i = 0; i < n; i++) {
if (n === 1 || n === 2) {
return 1;
} else {
return feibo(n-1) + feibo(n-2)}}}console.log(feibo(9));
/ / 34
Copy the code
2. Scope of variables
A variable defined inside a function using the keyword var (function scope) can only be used inside the function, not outside the function.
function fun() {
var num = 1;
console.log(num);
/ / 1
}
console.log(num);
// num is not defined
Copy the code
3. Local and global variables
Local variables: Variables defined in a scope (scope) are local variables of that scope and can only be accessed within the scope.
Global variables: Global variables are defined globally and can be accessed anywhere.
Variable declaration principle: global variables, after the global definition will be permanent, can be accessed at any time anywhere; The local variable is defined inside the function, and the process of function definition does not define the local variable. The local variable is defined only when the function is executed. After execution, the variable is destroyed immediately.
4. Scope chain
Scope chains refer to the rule that variables are found: they are named using the same identifier in different scopes. If the variable is in the current scope, use it directly; If not, the system searches layer by layer from this layer to the outer layer, using the first one found (proximity principle).
var num = 1;
function fun1() {
var num = 2;
function fun2() {
var num = 3;
console.log(num);
// Direct output 3 is defined in this layer
function fun3() {
console.log(num);
// This layer is not defined
}
fun3();
}
fun2();
}
fun1();
console.log(num);
// Output global 1
Copy the code
Parameters are local variables
function fun(a) {
console.log(a);
/ / 1
}
fun(1);
console.log(a);
// a is not defined
Copy the code
6. The role of global variables
- Transfer: Global variables can communicate between different functions (semaphore)
var num = 1;
function jia() {
console.log(num++);
}
function jian() {
console.log(num--);
}
jia(); / / 2
jia(); / / 3
jian(); / / 2
jian(); / / 1
jia(); / / 2
Copy the code
- Agree to call different functions
// Global variables will not be reset or cleared
var num = 1;
function plus() {
num += 4;
console.log(num++);
}
plus(); / / 5
plus(); / / 9
plus(); / / 13
Copy the code
7. Function scope
Function scopes are similar to variables in that they can only be used where the function is declared and cannot be accessed from anywhere outside the function
function outer() {
function inner() {
console.log(1);
}
inner();
}
outer();
/ / 1
inner();
// inner is not defined
Copy the code
8. Closure
Experience the closure
function outer() {
var num = 1;
function inner() {
console.log(num);
}
// inner does not have parentheses to indicate that only the definition of inner is printed, not executed immediately
return inner;
}
console.log(outer());
ƒ inner() {console.log(num); } * /
var fun = outer();
// fun is equivalent to the definition of inner (address)
fun();
/ / 1
// There is no definition of num in this layer, but inner has scope chain, so output 1
Copy the code
Closures: Functions encapsulate their internal statements and declared scopes into a closed environment
The function itself is a closure. When a function is defined, it can remember its external environment and internal statements, and each execution will refer to the closed environment at the time of definition.
Case 1
function outer(x) {
function inner(y) {
console.log(x + y);
}
return inner;
}
var fun = outer(3);
/* Fun = function inner(y) {console.log(3 + y); } * /
fun(5); / / 8
fun(8); / / 11
Copy the code
Case 2
function outer(x, y) {
function inner(x) {
console.log(x + y);
}
return inner;
}
var fun = outer(2.3);
/* the inner environment is x = 2; y = 3; Fun = function inner(y) {inner(y) = function inner(y); console.log(x + 3); } * /
fun(5); / / 8
fun(7); / / 10
Copy the code
Case 3
The closure of a function remembers the scope in which it was defined, and the variables in that scope are not immutable
function outer() {
var num = 1;
function inner() {
return num++;
}
return inner;
}
var fun = outer();
/* num = 1; Fun = function inner() {num = 1; return num++; } * /
console.log(fun());
/ / 1
console.log(fun());
/ / 2
console.log(fun());
/ / 3
Copy the code
Case 4
Each time a function is called, a new closure is created, with a new statement and a new external environment
function outer() {
var num = 1;
function inner() {
return num++;
}
return inner;
}
var fun1 = outer();
var fun2 = outer();
/* num = 1; Fun1 = function inner() {num = 1; return num++; } fun1 = function inner() {num = 1; return num++; } * /
console.log(fun1());
/ / 1
console.log(fun1());
/ / 2
console.log(fun2());
/ / 1
console.log(fun2());
/ / 2
Copy the code