JavaScript function
To achieve a particular function code segments together, code blocks can be reused.
Pros: make the program simpler and more logical conditioning calling more convenient and easier to maintain
Function declaration
The basic syntax
function 函数名([形参1,][形参2,]......){ //函数体 [return//返回值] }
Before and after the declaration can be called
Arguments (anonymous function)
var 变量=function([形参1,][形参2,]......){ //函数体 [return//返回值] }
After the declaration calls
Function is called
- Function name (); variable name ();
- Behind the event call
Since calls
(function () {alert (1 )}) ()Function overrides the self-declared variables function, to be called after the basic syntax of a function declaration statement, the statement can be called before and after the declaration
parameter
Dynamically change the function of the internal variable and the different results obtained with such a function thereof.
Parameter
() Function declaration inside the value when the value of the argument to accept
Arguments
When the value inside the function call (), the parameter assignment to
Parameter Description
- Parameter can be any data type
arguments object
In the object function inside the hermit created
Properties:
length
callee
access parameters passed specific values. arguments [index]
console.dir print object to the console
Function overloading
arguments get arguments
Different number of parameters or different types of parameters, so that a function of different functions
12.30
Scope of a function
Global Variables
在页面的任何地方都能访问到的变量,拥有全局的作用域
- The outermost layer of the variables defined functions
- There is no direct variable assignment with Global Properties
Local variables
只能在固定的代码片段中访问到
- Variables defined inside the function;
- Parameters are also local variables
return
To a function returns a value, and terminates the current function out
return return value
- The return value can be any data type
- You can only return a return value
- If no return value, the default value is undefined
recursive function
It calls itself inside the function