This is the 12th day of my participation in Gwen Challenge
function
Function declaration
Functions are functions.
We can wrap some statements inside of a function, and the function has a particular function.
Declare functions:
Function names are followed by parentheses in which arguments can be written, and function statements are written in parentheses to represent the whole.
A function declaration just tells us what a function does and it has statements, it doesn’t execute.
// Declare the function
function fun() {
// Write functional statements in braces to represent a whole
console.log(1);
console.log(2);
console.log(3);
}
Copy the code
A function call
The declaration of a function is not executed; the function must be called.
Call function: function name ();
The function is called as a whole, and the entire brace is executed.
// Declare the function
function fun() {
// Write functional statements in braces to represent a whole
console.log(1);
console.log(2);
console.log(3);
}
// Call the function
// Function name ()
fun();
Copy the code
Results:
A function is declared once and can be called many times. The execution position of a function is only related to the calling position and the declaration position.
fun();
console.log(4);
fun();
console.log(5);
// A function declaration can be called many times at a time. The execution position is independent of the declaration, but depends on the call
Copy the code
Results:
Function advantages: Function declaration can be called multiple times, simplifying code writing.
parameter
Functions can help us encapsulate some code, the code can be called repeatedly, the function leaves an interface, which is our parameter, can change the parameter to make our function different.
Parameters are variables and are named the same way as variable names
Formal parameters: When a function is declared, the formal parameters are written in parentheses. Line (reference)
Actual arguments: When a function is executed, the actual arguments (arguments) are written in parentheses
Parameter passing: Assigning actual parameters to formal parameters in a function call is called passing
Js is a dynamic statement, and the data type of the parameter changes dynamically, depending on the actual parameter type.
function sum(a, b) {
/ / function
console.log(a + b);
console.log(typeof a);
console.log(typeof b);
}
Copy the code
The actual parameters, and the number of formal parameters.
The function has an array object of class inside and arguments holds the actual arguments
1 Number of actual parameters < number of formal parameters. The argument is assigned to the preceding parameter first; the result of no assignment is undefined.
function fun(a,b) {
// a, b parameters
// arguments Saves the actual arguments
console.log(arguments);
console.log(a);
console.log(b);
}
// If the call has few arguments, the value is assigned to the previous argument first. Undefined is not assigned
fun(10);
Copy the code
Results:
2 Actual Parameters > Formal parameters, discard unnecessary actual parameters
Function advantage 2: The function has parameters, which is equivalent to providing us with an API interface, through which we can call the function and perform different operations. When encapsulating the function later, we only need to understand the purpose of the API, that is, the result after passing parameters, without understanding the structure of the function. You just need to know how to use your own function or someone else’s wrapped function.
return
Return value: RETURN can accept arguments as the return value of a function. Console.log () returns no output
// Declare the function
function sum(a,b) {
return a + b;
}
/ / call
console.log(sum(2.5));
Copy the code
The return value turns our function into an expression. Using this feature, we can pass a function as an actual parameter to another function.
The sum (10, the sum (15, 5))
Return feature: When a return is encountered inside a function, the function immediately returns, and subsequent statements are not executed.
// Return feature: The function returns immediately when it encounters a return inside
function fun() {
console.log(1);
return;
console.log(2);
console.log(3);
}
fun();
Copy the code
Results:
Function advantage 3: We can pass one function as a return value to another function. Good for our modular programming.
Modular programming
Since ancient times, human beings have been accustomed to dividing things, making some content into some common modules, which can be used over and over again.
Modular programming: Encapsulating basic common elements into a single function that can be called multiple times.
Note: modular programming can make our program more optimized. Each small module should have a single function as far as possible to improve the repetition rate.
function
Functional expression
Function expressions are also a way of declaring functions.
Assigns an anonymous function (the Ramda function) to a variable.
Function expressions must end with a semicolon.
var fun = function() {
console.log(10);
};
Copy the code
Call: function name (); fun();
The data type of the function
Simple data types: number,string, Boolean, undefined
Reference data types: Object,function
Function keyword, function expression data type is function
console.log(typeof fun);
console.log(typeof fun2);
Copy the code
Simple data types: a variable can receive any assignment. It can be assigned to another variable, essentially making a copy of the value stored in the variable. Changes to the variables do not affect each other.
var a = 10;
var b;
// A is a simple datatype, copy 10 and assign it to b
b = a;
// Change variables, others are not affected
a = 20;
// The variable can be assigned multiple times, saving the last time
console.log(a);
// Change of A does not affect B
console.log(b);
Copy the code
Reference datatypes: A variable can receive any assignment and can be assigned to another variable. If the variable holds a reference datatypes, it copies the pointer (address) of the variable to another variable, and changes to the variable affect each other.
// Reference the data type
var fun;
function fun2() {
console.log(1);
}
Fun2 = fun2; fun2 = fun2; fun2 = fun2
fun = fun2;
// The function can call the attribute, using = to assign the value
fun2.haha = "Ha ha";
fun.xixi = "Hee hee";
console.log(fun2.haha);
// Verify that fun can call haha
console.log(fun.haha);
// Verify that fun2 can call xixi
console.log(fun2.xixi);
Copy the code
Summary: Simple data types assign values, reference data types assign addresses.
Enhancement of function declarations
A variable can be used before it is declared, and the result is undefined (the variable declaration only promotes the declaration statement, not the assignment statement).
Functions can also be used first in the declaration, or can be promoted by the declaration.
If the function key is used to declare the function, the name of the function can be promoted before all statements, and since the function holds Pointers, the function definition can be found through Pointers, and no errors can be reported when used, and the function can be called normally. (Equivalent to promoting the whole function)
fun();
/ / statement again
function fun() {
console.log(1);
}
Copy the code
If a function expression is used to promote, only the variable name is promoted. The definition of the function cannot be promoted. An error is reported if the function is called first.
/ / to call first
fun2();
/ / define again
var fun2 = function() {
console.log(2);
}
Var fun2; Other statements execute fun2() where they were; fun2 = function() {console.log(2)}; * /
Copy the code
Conclusion: When declaring functions, we usually use the function keyword, which does not get an error.
And we usually write the function call first, with the declaration written after all statements, so that the code can read it.
The variable name is the same as the function keyword. The function name is promoted before the function name is used.
console.log(fun);
/ / after the statement
var fun = 10;
function fun() {
console.log(20);
}
Copy the code
The function expression has the same name as the function keyword, and the name is given precedence over the function keyword.
For example:
/ / to call first
fun();
/ / declare
var fun = function() {
console.log(1);
}
function fun() {
console.log(2);
}
/* Funciton fun; The fun name declares that var fun will not execute fun() in the same place as other statements; fun = function() {} */
fun();
Copy the code