This article mainly share with you to learn more about JS function, hope to help everyone better use JS function.
One, Custom function
1. Pass the Function keyword
function name ([parameter]) {
Code snippet;
return value;
}
Note: The function name does not contain special characters;
Function names are best defined;
Function names are best followed by hump marking or underline method;
Function names are strictly case-sensitive;
The function name, if repeated, will produce an overlay;
Functions can have parameters or can have no parameters;
The function returns the value by return, if no return returns the undefined by default;
function does not call not execute;
2. Anonymous functions
function expressions can be stored in variables, and variables can also be used as a function;
The anonymous function can be passed as an argument to other functions, and the receiver function can accomplish some functions through the function passed in.
Some one-time tasks can be performed by anonymous functions;
3. Through the function () constructor
Second, call the function
1. As a function call
Called by the function name (), if there are parameters to pass the corresponding parameters;
The default global object in HTML is the HTML page itself, so the function belongs to the HTML page, and the Page object in the browser is the Browse window. So the function will automatically become a function of the Window object, or it can be called through the Window function ().
2. Global Objects
When the function is not called by its own object, the value of this becomes the global object, and the global object in the Web browser is the browser window to the Windows object;
When a function is called as a global object, the value of this is changed into a global object, and using the Window object as a variable can cause the program to crash;
3. function as method call
You can call a method that defines a function as an object;
4. Calling a function using the constructor function
If the new keyword is used before a function call, the constructor is called;
5. As a fallback function call
Call ();
Apply ();
Three, parameters
The function can have parameters, or no parameters, if the parameters are defined, there is no value in the function call, the default setting is undefined;
If the passed parameter exceeds the defined parameter when the function is called, JS will automatically ignore the extra parameters;
JS can not directly write the default value, the argument object to achieve the default value effect;
The function of variable parameters can be realized by arguments object;
Changing the variables in the function body by value passing parameters does not affect the variable itself;
Changing the variables in the function body through the object passing parameters will affect the variable itself;
Iv. Scope of variables
1. Local Variables
A variable declared in the body of a function, used only within a function body;
2. Global variables
A variable declared in the body of a function that can be used at the beginning of a variable declaration to the end of a script;
3. Note
Try to control the number of global variables, easy to cause bugs;
It is best to always use the VAR statement to declare variables;
The global function in the five and JS
1.parseInt (String,radix)
Returns the value converted to an integer;
2.parseFloat (sring)
Returns a value converted to a floating-point type
3.isFinite (value)
Detects if a value is an infinite value, and returns False if number is Nan or infinity or-infinity;
4.isNaN (value)
Detects if a value is Nan and returns True if the value is Nan, otherwise false;
5.encodeURL (URL)//encodeuricomponent ()
Encodes a string as a url,ascii punctuation mark, and the function is not escaped,/? : @&+=¥#, the encodeURIComponent () method can be used to encode the ASCII punctuation mark with special meanings;
6.decodeuri//decodeuricomponent ()
Decodes an encoded URI;
7.escape ()
Encode the string;
The escape () function encodes the string so that it can be read on all computers;
Numbers and letters are not encoded, and the following punctuation marks are not encoded, *@-_+./all other characters will be replaced by escape sequences;
The escape () function cannot be used to encode URIs
8.unescape ()
Decoding a function that has escape encoding
9.eval ()
Execute the JS string as a script
If the argument is an expression, the eval () function executes the expression, and if the argument is a JS statement, the JS statement is executed;
The eval () function is a dynamic code executed by a function that is much slower than executing the script directly;
Use the eval () function carefully, as far as possible, to ensure the security of the program;
10.Number (obj)
Converts the value of the object to a number;
If the value of the object cannot be converted to a number, Nan is returned;
If the object is a Date object, returns the number of milliseconds elapsed from January 1, 1970 to the limit;
11.string
Converts the value of an object to a string, as with ToString;