Functions in JavaScript _ javascript skills

Source: Internet
Author: User
A function is defined by the keyword function and function name plus a set of parameters and a semantic definition to be executed in braces. Today, we will explain in detail the functions in JavaScript. The keyword function is used to define a function.

The code is as follows:


// Function declarative definition:
Function funcname ([arg1 [, args [..., argn]) {
Statements
}
// Function expression definition:
Var funcname = function ([arg1 [, args [..., argn]) {
Statements
};

Note that curly braces in function statements are required, even if the function body contains only one statement.

In JavaScript, a Function is a specific instance of the Function class. They all have the same attributes and methods as other reference types.

The function name is actually a pointer to a function object. a function can be used as a parameter to pass parameters and return values.

Object features of functions

Because a Function is a Function instance, the Function name is only a reference address of the instance. Therefore, parameters and return values can be involved in the parameter passing process of the function.

The code is as follows:


Function call_some_function (some_function, some_argument ){
Return some_function (some_argument );
}
Function add_10 (num ){
Return num + 10;
}
Console. log (call_some_function (add_10, 20); // 30

Internal attributes of a function

Arguments | this
• The parameters passed to the function are stored in the arguments object.
• Arguments. length returns the number of input parameters.
• Note: The length attribute indicates the number of parameters received by default during function definition. Arguments. length indicates the number of parameters received when the function is actually executed.

The code is as follows:


Function test_arguments (){
If (arguments. length = 2 ){
Console. log (arguments. length );
Console. log (arguments );
} Else {
Console. log (arguments. length );
Console. log (arguments );
Arguments. callee (4, 5 );
};
} (1, 2, 3)
/**
3
{'0': 1, '1': 2, '2': 3}
2
{'0': 4, '1': 5}
**/

• Arguments. callee () is mainly used to call the function itself in recursive functions. The difference between js and other languages is that the function name is just a pointer and can be changed at any time. using the function name to call the function itself is highly coupled and may cause problems. arguments. callee () calls itself to avoid this problem.

The code is as follows:


Function factorial (num ){
If (num <= 1 ){
Return 1;
} Else {
Return num * factorial (num-1 );
};
}
Function callee_f (num ){
If (num <= 1 ){
Return 1;
} Else {
Return num * arguments. callee (num-1 );
};
}
Factorial (10); // normal operation
F = factorial;
Factorial = null;
F (10); // error
Callee_f (10); // normal operation
F = callee_f;
Callee_f = null;
F (10); // normal operation

• This is mainly used to help the function reference objects in the function scope.

The code is as follows:


Var color = 'red ';
Function syaColor (){
Console. log (this. color );
}
SyaColor (); // red
Var o = new Object ();
O. color = 'Blue ';
O. sayColor = sayColor;
O. sayColor (); // blue

Call () and apply ()

Call () and apply () are the methods that every function contains. Previously, we have mentioned that a function is a defined object. when calling a function, this is a call to the current and next variables in the function. If you want to change the domain space of function execution, you can use call () and apply.

The code is as follows:


Color = 'red ';
Var o = {color: 'blue '};
Function sayColor (){
Console. log (this. color );
}
SayColor (); // red
SayColor. call (this); // red
SayColor. call (o); // blue

App () and call () have the same role, but the difference mainly lies in the differences between input parameters.

Call (this, para1, prar2, prar3) the first parameter is the scope of the function to be executed. the following parameter is the input parameter of the function, and the number of sequential writes.

Apply (this, [para1, para2, prara3]) The first parameter is also the scope of the function to be executed, followed by an Array object.

The biggest benefit of using call ()/apply () to expand the scope is the decoupling of objects and methods.

Built-in object

Global objects can be understood as the outermost objects, all objects, and attributes and methods that do not belong to other objects are included in Global objects.
* IsNaN (x) is used to check whether the parameter x is a number. If it is a number, false is returned; otherwise, true is returned.
* IsFinite (x) is used to check whether the parameter x is infinite or small. if it is infinite or small, true is returned.
* ParseInt (x) is used to parse a string and return an integer.
* ParseFloat (x) is used to parse a string and return a floating point number.
* EncodeURI () and encodeURIComponent () perform special UTF-8 encoding on the string to avoid special characters for the browser to understand. The difference between the two is that encodeURI () does not encode special characters belonging to the URI, while encodeURIComponent () encodes all non-standard characters it discovers.

The code is as follows:


Var uri = "http://www.wrox.com/illegal value.htm # start ";
// Http://www.wrox.com/illegal%20value.htm#start
Console. log (encodeURI (uri ))
// Http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm % 23 start
Console. log (encodeURIComponent (uri ))

• The corresponding decoding functions are decodeURI () and decodeURIComponent ()
• Eval (script) is used to execute the script content in the interpreter and return the corresponding results. Very powerful!

Note: in a browser, windows objects encapsulate Global objects and undertake many additional tasks and functions.

The Math object is another built-in object. It provides mathematical computing for JavaScript.

The above is all the content in this article. I hope my friends will like it and help you.

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.