13.8.5 instance attributes and class attributes of functions
The javascirp function is not only a function, but also a ray. According to the way variables are declared in the function, there are 3 variables in the function.
Local variables: variables declared in common mode in functions.
Instance property: Variable prefixed with this in the function.
Class Attribute: a variable prefixed with a function name in a function.
13.8.6 how to call function 3
After defining a function, JavaScript provides the method for calling the function in step 3.
Directly call a function
// Call the alert method of the window object
Window. Alert ("test code ");
Call a function using the call () method
Directly calling a function is simple and easy to use, but this call method is not flexible enough. Sometimes, when calling a function, you need to dynamically input a function reference,
To call a function dynamically, you must use the call method to call the function.
Code example:
VaR each = function (array, FN)
{
For (VAR index in array)
{
// Use window as the caller to call the FN Function
// Index and array [Index] are parameters passed to the FN function.
FN. Call (null, index, array [Index]);
}
}
// Call the each function. The first parameter is an array and the second parameter is a function.
Each ([4, 20, 3], function (INDEX), ELE)
{
Document. writeln ("the" + index + "element is:" + Ele );
}
Call a function using the apply () method
The apply () method and the call () method have similar functions. They can call functions dynamically.
The difference between apply () and call () is as follows:
When calling a function through call (), each parameter must be listed in detail in parentheses.
When using apply () to call a function dynamically, arguments can be used in parentheses to represent all parameters.
Code example:
VaR myfun = function (a, B)
{
Alert ("the value of A is:" + A + "/the value of Nb is:" + B );
}
VaR example = function (num1, num2)
{
// Directly use arguments to represent all parameters passed in when the example function is called
Myfun. Apply (this. Arguments );
}
// Input an array for apply () dynamic call
Myfun. Apply (window, [12, 23]);
Example (20, 40 );
13.8.7 function independence
Although a function can be defined as a class method or an object method. However, JavaScript Functions are always independent,
Functions will never belong to other classes or objects.
Code example:
Function dog (name, age, bark)
{
This. Name = Name;
This. Age = age;
This. Bark = bark;
This.info = function ()
{
Return this. Name + "the age is:" + this. Age + ", and its cry is:" + this. bark;
}
}
// Create a dog instance
VaR dog = new dog ("wangcai", 3, "Wangwang ");
// Create a cat function, corresponding to the cat class
Function CAT (name, age)
{
This. Name = Name;
This. Age = age;
}
// Create a cat instance
VaR cat = new CAT ("kitty", 2 );
// Separates the info method of the dog instance and calls the info method through the call method.
// At this time, cat is the caller
Alert (dog.info. Call (CAT ));
The output result is: the age of Kitty is 2, and its cry is undefined.
The above program function dog defines the instance method named Info (), but this info () method does not completely belong to the dog instance, it is still an independent function, all programs in the last line of code
And let the cat instance call the info () method.
13.9 function parameter Processing
Most of the time, the function must accept parameter passing. All parameters of javasripare passed by value.
13.9.1 parameter transfer of basic and composite types
For basic type parameters, JavaScript uses the value transfer method. Therefore, modifying the parameter value in the function does not affect the real parameters.
Sample Code for basic type parameters:
Function Change (Num)
{
Num = 5;
Document. Write ("num =" + num in the function );
}
VaR num = 1;
Change (Num );
Document. Write ("the num of the input function is" + num );
Sample Code for composite parameters:
Function Change (person)
{
Person. Age = 10;
Document. writeln ("the age value of person in function execution is:" + person. Age );
}
// Define the person object using JSON syntax
VaR person = {age: 5 };
Document. writeln ("the age of the person before the function call is:" + person. Age );
Change (person );
Document. writeln ("the age value of person after function call is:" + person. Age );
Result: the age of the person before the function call is 5.
The age value of the person for the function execution value is 10.
The age value of the person after the function is called is 10.
13.9.2 null Parameter
In JavaScript Functions, the parameter values that do not pass in real parameters are automatically set to undefined values.
Code example:
Function changeage (person)
{
If (typeof person = 'object ')
{
Document. Write (person. Age );
}
Else
{
Alert ("the parameter type does not match:" + typeof person );
}
}
The output result is: the parameter type does not match: undefined.
Because JavaScript does not require input parameters when calling a function, JavaScript does not have the so-called function "overload". If two functions with the same name are defined successively,
Their parameter lists are not the same, which is not a function overload. This method causes the function defined later to overwrite the previously defined function.
Code example:
Function Test ()
{
Alert ("the first test function ");
}
Function Test (name)
{
Alert ("second function with Parameters ");
}
Test ();
The output result is never the second test function.
13.9.3 parameter type
The list of parameters declared by JavaScript Functions does not require type declaration, which is a feature of a weak type language.
Code example:
// The name can be of any type, but it does not need to be declared in the function.
Function Test (name)
{
}