- Functional keyword function
Review
Var is the keyword of JS, used to declare variables, declared in memory module completion, definition (=) is done in the execution module.
var can be done in advance of the memory module (before the JS code executes) so there are variables to promote this function.
Because a does not have Var, there is no variable promotion, and the browser treats him as a property of window.
function is also the keyword of JS, for declaring and defining functions, declarations and definitions are completed in the memory module.
function is also the memory module is completed ahead of time, so there are functions to promote this function.
Variable promotion and function promotion are called pre-interpretations. are browser-only.
2. Function definition: function: A block of code with certain functions.
functions are defined by functions, followed by function names.
Structure syntax
function fn () {}
We can find that the memory module only exists var variable name. Instead, the entire block of code is stored, because declarations and definitions are done at the same time.
The attention of the function structure
function definitions define function fn () {} by function
Four ways to define a function
- Declarative definition function fn () {}
- Expression-defined var an=function () {} anonymous function
- Self-invoking
Both definitions and calls are completed at the same time.
4. Through the form of the object Var as=new Function () {}
function call Function name () FN ()
The biggest advantage of the function is that it forms a separate module that can be used on demand and can be reused.
Use on demand reduces code redundancy (Rong) redundancy by improving performance reuse.
Procedure for function calls
When the function executes, it forms a private scope of its own. The largest scope is window (global scope)
Then execute the code block inside the function.
Scope chain: When a variable inside a function is used, the variable is first looked up under its own private scope, there is a direct use, no one will be found to his upper level, the parent uses the parent, and the parent does not continue to look up until the window, the window has no use is Not defined. This lookup mechanism we call the scope chain.
The function belongs to who, and where she is called has no relationship, but in which definition is concerned.
function recursion: Inside the function internally calls itself
Break and Continue operations
Break terminates the current program
Continue jumps out of the current program and executes the next
Console.log (a)
Console.log (b)
var a=12;
b=12;
function fn () {
Console.log (12)
//}
FN ()
function fn () {
Console.log (123)
//}
As ()
var as=function () {
Console.log (12)
//}
var a=12;
function fn () {
Console.log (a)
var a=144;
Console.log (a)
//}
//
function fn () {
Console.log (123);
function ff () {
Console.log (52)
}FF ()
}FN ()
FN ()
var sum=0;
var I=1;
function fn () {
Sum=sum+i;
i++;
Console.log (SUM)
FN ()
//}
FN ()
(function () {
Console.log (412)
//})()
!function () {
Console.log (12)
//}()
var a = 123;
function Fun () {
alert (a);
A = 456;
}
Fun ();
alert (a);
Definition of number 10.9th function