First, function declaration
1. Custom Functions
function Fun1 () {
Alert ("I am a custom function");
}
Fun2 ();//function does not call, does not execute itself
2. Direct Volume declaration
var fun2=function () {
Alert ("Direct volume Declaration");
}
Fun2 ();
3, using the Function keyword declaration
var fun3=new Function ("var a=10;b=20;alert (a+b)");
Fun3 ();
Second, variable declaration promotion
If a variable is declared inside a function body, the variable declaration is promoted to the beginning of the function, but no value is assigned, regardless of whether the variable function is external or not.
Declaring a variable inside a function body promotes the declaration to the top of the function body. However, only the variable declaration is lifted, and no value is assigned.
var num=10;
Fun1 ();
function Fun1 () {
Console.log (num);
var num=20;//variable Promotion
}//undefined
Equivalent
var num=10;
Fun1 ();
function Fun1 () {
var num;
Console.log (num);
num=20;
}
Third, function to pass the parameter
The number of function arguments is the same as the number of formal parameters,arguments.length can get the number of function arguments
1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "UTF-8">5 <title>Title</title>6 <Script>7 /*variable Promotion*/8 /*window.onload=function () {9 var num=10;Ten fun1 (); One function fun1 () { A console.log (num); - var num=20;//variable Promotion - } the }//results: undefined*/ - - /*var a=18; - F1 (); + function F1 () { - var b=9; + Console.log (a); A Console.log (b); at var a= ' 123 '; - }//undefined,9 - */ - /*function Pass parameter*/ - /*function F1 (A, b) { - Console.log (a+b); in } - F1 (//3); to F1 (5);//nan*/ + - /*detection function parameter number matching*/ the functionfn (A, b) { * Console.log (fn.length);//get the number of function parameters $ Console.log (arguments.length);//The number of arguments is obtainedPanax Notoginseng if(Fn.length==arguments.length) { - Console.log (A+b); the }Else{ + Console.log ("Sorry, your parameters do not match, the correct number of parameters is:"+fn.length); A } the } + fn (5,3);//2,2,8 - fn (2,3,4)//2, 3, "Sorry ..." $ $ - </Script> - </Head> the <Body> - Wuyi </Body> the </HTML>
Day 24th: js-function Variable Declaration promotion