A. function
The functions in JavaScript are very similar to those in Python, but there are some differences in how they are defined.
1. Define functions;
A function can only return a value, and if you want to return more than one value, you can only return it in an array or object.
①. Common functions:
Function F1 () {
Console.log ("Hello World");
}
F1 (); Calling methods
②. Functions with Parameters:
function F2 (A, b) {
Console.log (arguments); Built-in arguments objects
Console.log (arguments.length);
Console.log (A, b);
}
F2 (A, b); Calling methods
③. Functions with return values:
function sum (A, b) {
return a+b;
}
Sum (A, b); Calling methods
④. Anonymous functions:
var sum = function (A, b) {
return a+b;
}
Sum (A, b); Calling methods
⑤. Functions for immediate execution:
(function (A, b) {return a+b}) (A, B);
※. ES6 allows you to define functions using the arrow (= =):
var f = v + V;
F: Name of function
V: Function parameters (left arrow) if the function has multiple arguments or no arguments, this position is replaced with parentheses
V: function return (arrow right)
2. Arguments parameters of the function:
function F2 (A, b) {
Console.log (arguments.length);
Console.log (A, b);
}
F2 (2,3);
Output results: 2
2 3
A function that uses the built-in arguments parameter can accept any number of arguments, even if the function sets the number of arguments.
3. Global variables and local variables for functions:
①. Global variables:
Variables declared outside the JavaScript function are global variables, and all scripts and functions on the Web page can access it.
②. Local variables:
A variable declared inside JavaScript ambiguity (using VAR) is a local variable, so it can only be accessed inside the function (the scope of the variable is inside the function). Local variables are deleted as soon as the function is complete.
③. The life cycle of variables;
The life cycle of JavaScript variables begins with their being declared.
Local variables are deleted after the function has finished running.
Global variables are deleted after the page is closed.
④. Scope of variables:
The function lookup variable is first found inside the function, the outer look is not found, and the outermost layer is discovered gradually.
4. Lexical analysis
In JavaScript, there is a momentary time to parse a function before calling it.
Inside the function, either use a parameter or use a local variable to find it on the AO.--*/
Lexical Analysis Process:
When a function is called, an active object is formed, or AO, in the first moment.
①. Function set parameter, added to AO attribute, and value is undefined, i.e. ao, parameter = undefined
The function runs the receive argument, adds to the Ao property, overwrites the previous undefined value
②. If AO does not add a variable attribute when parsing a parameter, the variable attribute is added to the AO and the value is undefined, which is ao. variable = undefined
If the AO already has a variable property when parsing the parameter, the variable property is not modified.
③. If a function that has the same variable name as the parse variable is declared inside the function, the corresponding variable is replaced with a function. If no function is declared or the variable name of the declared function and parse variable is not the same, no action is done.
5. Built-in objects and methods
Everything in JavaScript is an object: strings, numbers, arrays, dates, and so on. The object is the data that owns the property and method.
Type |
Built-in objects |
Introduced |
Data type |
Number |
Numeric objects |
String |
String Object |
Boolean |
Boolean-Valued Object |
Group objects |
Array |
Array Object |
Math |
Mathematical objects |
Date |
Date Object |
Advanced objects |
Object |
Custom Objects |
Error |
Error Object |
Function |
Function object |
Regexp |
Regular Expression Object |
Global |
Global objects |
Advanced JavaScript in the front