Table of Contents
This article mainly shares with you a further understanding of js functions, hoping to help everyone use js functions better.
1. Custom function
If the parameters passed exceed the parameters defined when calling the function, js will automatically ignore the excess parameters;
Variables declared within the function body are only used within the function body;
Return the value converted into an integer;
Home Backend Development PHP Tutorial Learn more about js functions

Learn more about js functions

Mar 19, 2018 pm 03:47 PM
javascript learn further


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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

At a glance: A quick overview of how to open JSP files At a glance: A quick overview of how to open JSP files Jan 31, 2024 pm 09:28 PM

JSP file opening method JSP (JavaServerPages) is a dynamic web page technology that allows programmers to embed Java code in HTML pages. JSP files are text files that contain HTML code, XML tags, and Java code. When a JSP file is requested, it is compiled into a JavaServlet and then executed by the web server. Methods of Opening JSP Files There are several ways to open JSP files. The easiest way is to use a text editor,

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Differences between Go language and Golang: Do you know it? Differences between Go language and Golang: Do you know it? Feb 24, 2024 pm 06:06 PM

Go and Golang are the same programming language and there is no substantial difference between them. Go is the official name of the programming language, and Golang is the abbreviation commonly used by Go language developers in the Internet field. In this article, we will explore the characteristics, uses, and some specific code examples of the Go language to help readers better understand this powerful programming language. Go language is a statically compiled programming language developed by Google. It has the characteristics of efficiency, simplicity, and strong concurrency, and is designed to improve programmers' work efficiency.

See all articles