Home  >  Article  >  Backend Development  >  Learn more about js functions

Learn more about js functions

小云云
小云云Original
2018-03-19 15:47:081382browse


This article mainly shares with you a further understanding of js functions, hoping to help everyone use js functions better.
1. Custom function

1. Through the function keyword

function function name ([parameter]){

Code segment;

return return value;

}

Note: The function name should not contain special characters;

It is best to have a clear meaning of the function name;

Function name is best to follow the hump markings or lower lines;

function names are strictly distinguished by the case; It can have parameters or no parameters;

The function returns a value through return. If there is no return, it returns undefined by default;

The function will not be executed if it is not called;

2. Anonymous function

Function expressions can be stored in variables, and variables can also be used as a function;

Anonymous functions can be Passed as a parameter to other functions, the receiver function can complete certain functions through the passed function;

Can perform certain one-time tasks through anonymous functions;

3. Construct the function through Function()

2. Call the function

1. Call it as a function

Through the function name ( ) to call, if there are parameters, just 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 browsing window (window ). Therefore, the function will automatically become a function of the window object, and can also be called through window.function();

2. Global object

When the function is not called by its own When the object is called, the value of this will become the global object. In the web browser, the global object is the browser window to the window object; When the function is called as a global object, the value of this will become the global object. Object, using the window object as a variable can easily cause the program to crash;

3. Call the function as a method

You can define the function as a method of the object to call;

4. Use the constructor to call the function

If the new keyword is used before the function call, the constructor is called; 5. As a response Drop function calls

call();

apply();

3. Parameters

Functions can have parameters or no parameters, if defined Parameters, no value is passed when the function is called, and the default setting is undefined;

If the parameters passed exceed the parameters defined when calling the function, js will automatically ignore the excess parameters;

Default values ​​cannot be written directly in js. Default value effects can be achieved through arguments objects;

Functions with variable parameters can be implemented through arguments objects;

Pass parameters by value and modify variables in the function body Making modifications will not affect the variable itself;

Passing parameters through objects and changing the variable pair in the function body will affect the variable itself;

4. Variable scope

1. Local Variables

Variables declared within the function body are only used within the function body;

2. Global variables

Variables declared outside the function are used from the beginning of the variable declaration to the end of the script. Can be used;

3. Note

Try to control the number of global variables, which may easily cause bugs;

It is best to always use the var statement to declare variables;

5. Global functions in js

1.parseInt(string,radix)

Return the value converted into an integer;

2.parseFloat(sring)

Return the value converted into floating point type

3.isFinite(value)

Detect whether a value is infinite, if number is nan or infinity or -infinity number, then return false;

4.isNaN(value)

Detect whether a value is NaN, if the value is nan, return true, otherwise return false;

5.encodeURL(url)//encodeURIComponent()

Encode the string into url, ASCII punctuation, this function will not escape,,/? : @&+=¥#, you can use the encodeURIComponent() method to encode ASCII punctuation marks with special meanings respectively;

6.decodeURI//decodeURIComponent()

Decode a certain encoding URI;

7.escape()

Encode the string;

The escape() function can encode the string so that it can be read on all computers Get the string;

will not encode numbers and letters, nor will it encode the following punctuation marks. *@-_+./ All other characters will be replaced by escape sequences;

The escape() function cannot be used to encode URI

8.unescape()

Decode the function with escape encoding

9.eval()

Execute js string as a script

If the parameter is an expression, the eval() function will execute the expression. If the parameter is a js statement, the js statement will be executed;

The eval() function is a dynamic code executed by a function, which is much slower than directly executing the script;

Use the eval() function with caution and try not to use it to ensure the security of the program;

10.Number(obj)

Convert the value of the object into a number;

If the value of the object cannot be converted into a number, NaN is returned;

If the object is date object, returns the number of milliseconds elapsed from January 1, 1970 to the limit;

11.string

Convert the value of the object into a string; and Same as toString;

Related recommendations:

Explanation of promotion and closure of js functions and variables

In-depth analysis of JS functions

Summary and sharing of knowledge points related to js functions

The above is the detailed content of Learn more about js functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn