First, what is the function?
It is a function code for performing certain functions, in order to reduce the code reuse, a function used as the code used when needed at any time to call.
Second, the declaration of the function (function name strictly case-sensitive)
1.function command
function function name (variable parameter) {
body of the function
return value is returned
}
2. function expression (variable assignment to take written)
var function(s){
console.log(s);
};
The variable assignment and function, configured as a function expression.
3.function Constructor
var add = new Function(
'x',
'y',
'return x + y'
);
// 等同于
function add(x, y) {
return x + y;
}
Third, the repeated declaration of the function (when the function name is repeated, the latter function declaration overwrite the previous function)
function f() { console.log(1); } f() function f() { console.log(2); } f() // 2
Fourth, the parentheses operator
function add(x, y) { return x + y; } add(1, 1) // 2
return action: A: Quit function execution; b: return a result, as the function returns no result, undefined is returned
Fifth, the realization in the form of default parameters
Example function (name, Age) {
name = name || 'Demacian';
Age = Age || 18;
Alert ( '! Hello I am a' + name + ', this year' + age + 'years old.');
}
Six variable parameter function of the form
function text1(){ var paramsNum=arguments.length; var max=0; for(var i=0;i<=paramsNum-1;i++){ max=arguments[i]; } } return max; } aletr(test1(123,3423,23456,6666666));
Seven, scope, global and local variables
Programs range anywhere can access: 1. global scope
Global variables: save, variables can be used anywhere in the program's global scope, can be used repeatedly, as long as hope everywhere available public variables, which they do not belong to any function that automatically are global,
2. Scope function: the function is only available in the range
Local variables: the function stored in scope, the only variables available functions, can not be used repeatedly, is only available in the current function, local variables the outer two functions are not available: 1, the variables declared in the function 2, function parameter variables are local variables
3. Use variables order: priority to use local variables within a function, not a local, before going global look.