Javascript learning 5-Functions

Source: Internet
Author: User

In JavaScript, functions and objects are intertwined. Some functions are associated with objects. This is discussed in Part 6.
This section mainly discusses the differences between functions and other familiar languages (such as C/C ++ ).

5.1 Function Definition
Function Name (parameter 1, parameter 2 ...)
{
Subject;
}
The function returns the undefined value if return or no return is returned.
In addition, JavaScript is a loose language. Therefore, parameter passing during function calls has the following features:
① JavaScript will not detect whether the transmitted data is of the type required by the function.
If the data type of the parameter is very important, you can use the typeof operator in the function for detection.
② JavaScript does not check whether the parameters passed to it are correct.
If more parameters are passed than the required number, the excess values are ignored.
If the number of parameters passed is less than the required number, the ignored parameters are assigned the undefined value.
③ The function has an arguments object that maintains the parameters actually passed to the function.
You can use it to obtain the number of parameters actually passed to the function and the parameter value. It is used to implement variable parameter functions.

5.2 nested Functions
The implementation is as follows:

1 Function Hypotenuse (A, B) {
2 Function Square (X) {ReturnX*X ;}
3 Return Math. SQRT (square () + Square (B ));
4 }

5.3 function parameters
● Optional parameters
Because it does not check whether the parameters passed to it are correct, you can ignore the corresponding parameters to achieve the purpose of optional parameters.

1 Function Copypropertynamestoarray (O, /**/ /*Optional*/ A) {
2 If ( ! A) = []; // If undefined or null, use a blank Array
3 // The preceding statement can also write a = A | [];
4 For ( VaR Property In O) A. Push (property );
5 Return A;
6 }

● Variable parameters
In a function, the identifier arguments has a special meaning. It is a special attribute that references the arguments object.
It is similar to an array object. You can obtain the parameter values passed to the function according to the number.
Arguments has the Length attribute to obtain the number of parameters actually passed to the function.
Obtain the first parameter value, which can be obtained using argments [0.

1 Function Max ( /**/ /**/ )
2 {
3 VaR M = Number. negative_infinity;
4 // Loop through all the arguments, looking for, and
5 // Remembering, the biggest
6 For ( VaR I =   0 ; I < Arguments. length; I ++ )
7 If (Arguments [I] > M)
8 M = Arguments [I];
9 // Return the biggest
10 Return M;
11 }

Arguments has the callee attribute and references the currently executed function.
Arguments is not a real array. It is an arguments object.
5.4 data Functions
The most important thing about functions is that they can be defined and called. The definition and call are Javascript and mostProgramThe syntax of the language.
However, in Javascript, a function is not just a syntax, but can also be viewed as data. This means that you can assign a function to a variable, store it in the object's attributes or array elements, and pass it as a parameter to the function.
Example 1: assign a function to a variable:

1 Function Square (X) {ReturnX*X ;}
2 VaR A = Square ( 4 ); // A contains the number 16
3 VaR B = Square; // Now B refers to the same function that square does

Example 2: stored in object attributes or array elements

1 VaR O =   New Object;
2 O. Square =   Function (X) {ReturnX*X ;} // Function literal
3 Y = O. Square ( 16 );
4 VaR A =   New Array ( 3 );
5 A [ 0 ] =   Function (X) {ReturnX*X ;}

For more information, see the example at the end of section 8.3 of the Javascript authoritative guide.

5.5 functions used as methods and this keyword
The object method is only an attribute of the object, and the property references the function type.
Because a function can be assigned to any variable and any attribute of an object.
 

1 VaR Calculator =   { // An object literal
2 Operand1: 1 ,
3 Operand2: 1 ,
4 Compute: Function () {
5This. Result= This. Operand1+ This. Operand2;
6}
7 } ;
8 Calculator. Compute (); // What is 1 + 1?
9 Print (calculator. Result ); // Display the result

Above, the this keyword is very important. Any function used as a method is effectively passed an implicit parameter, that is, the object that calls the function (this ).

5.6 attributes and methods of functions
A function is a special type of JavaScript objects. When the typeof operator is used for function types, the string "function" is returned"
Since a function is an object, it has attributes and methods, just like other objects.
● Attribute lenght
Different from the Length attribute of arguments in a function, The Length attribute of the function itself is a read-only attribute and returns the actual number of parameters required by the function.
● Define the attributes of a function and have static attributes.

1 // Create and initialize the "static" variable.
2 // Function declarations are processed before code is executed, so
3 // We really can do this assignment before the function declaration.
4 Uniqueinteger. Counter =   0 ;
5
6 // Here's the function. It returns a different value each time
7 // It is called and uses a "static" Property of itself to keep track
8 // Of the last value it returned.
9 Function Uniqueinteger () {
10ReturnUniqueinteger. Counter++;//Increment and return our "static" variable
11}

● Method apply () and call ()
These two methods can call functions just like calling other methods. The first parameter is the function object to be called. In the function body, this parameter is the value of the keyword "this.
The remaining parameters of call are the values passed to the function to be called.
To the function f () and invoke it as if it were a method of the object o, you cocould use code like this:
F. Call (O, 1, 2 );
This is similar to the following lines of code:
O. M = F;
O. m (1, 2 );
Delete o. m;
The apply () method is like the call () method, Except t that the arguments to be passed to the function are specified as an array:
F. Apply (O, [1, 2]);
For example, to find the largest number in an array of numbers, you can use the apply () method to pass the elements of the array to the math. Max () function:
VaR biggest = math. Max. Apply (null, array_of_numbers );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.