javascript---something about function

Defining

  • the name of function
  • zero or more parameter names contained with parenthesis,each separated by commas
  • the JavaScript statements that comprise the body of the function,contained with curly braces

Note the English word:parenthesis,comma and curly braces,which are '()',',' and '{}'.

a recusive function example

 

  
    
function factorial(x)
{
if (x <= 1 )
{
return 1 ;
}
return x * factorial(x - 1 )
}

 

when a function has been defined,it may be invoked with the () operator easily;

You konw,JavaScript is a loosely type programming language,you are not expected to check the data type for the function parameters,so you can invoke the above function like this:

factorial('1')、

factorial('a')、

factorial(1) and so on

so if the data type of a function parameters are important(most of time ,it does),so you can check the data type yourself by the typeof operator,if it is not the wanted datatype,you can do something ,like thowing a exception,eg:

 

代码
   
     
function factorial(x)
{
if ( typeof x != " number " ) // check the data type yourself
{
// if it is not the wanted datatype ,throw a exception yourself
throw " unwanted datatype "
}
if (x <= 1 )
{
return 1 ;
}
return x * factorial(x - 1 )
}

beacuse of the loosely property,so JavaScript even does not check whether you have passed the correct number of the function parameters to a function.if you pass more  arguments than the function expects,the function will ignores the extra arguments automatically,if you pass less than expected,the function set the parameters that you ommi to undefined value;

Arguments

    most of time,it is very important to know whether there is the correct number of parameters are passed or not,so how do I know correct or not,don't worry,JavaScript have a special property that refers to an object known as Arguments,we can use it like follows:

 

代码
   
     

function f(x,y,z)
{
// check the number of the arguments passed with arguments.length
if (arguments.length != 3 )
{
throw " I need 3 paraments " ;
}
else
{
// access the certain argument by arguments[index],index should between 0 and arguments.length-1
alert(arguments[ 0 ]);
alert(arguments[
0 ]);
alert(arguments[
0 ]);

}
}

oh my god,the arguments is so similar to a array,Note it is  just similar not same,the arguments is just an array-like object.Remember it is not really an array,it is better to think of it as an object that happens to have a length property and numbered property.the arguments does not have any other array property and method,like reverse and so on;

callee property

 the arguments has another property named callee,which refers to the function that is currently being executed.this property is rarely useful,but it can be used to allow unnamed functions to invoke themselves recursively,so the above recursive function can be here like this:

 

  
  
    
function (x)
{
if (x <= 1 )
{
return 1 ;
}
return x * arguments.callee(x - 1 );
}

welcome to shanghai for Sweat,the air-conditioning is 19 degrees,my house is so cool,but if you are out the house,you will die,it is a burner.

你可能感兴趣的:(JavaScript)