Home>Article>Web Front-end> How to define a javascript function

How to define a javascript function

青灯夜游
青灯夜游 Original
2021-06-28 17:17:46 5214browse

How to define a function in JavaScript: 1. Use the "function function name (parameter list) {execution code}" statement; 2. Use the "var variable name = function (parameter list) {execution code}" statement; 3. Use the statement "Document object.event=function(){function body}".

How to define a javascript function

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Method 1: Function declaration--named function

The basic syntax is as follows:

function 函数名([参数列表]){ 函数体; [return [表达式;]] }

Method 2: Function expression--anonymous Function

The basic syntax is as follows:

var fn = function([参数列表]){ 函数体; [return [表达式;]] }

The function expression assigns the anonymous function to a variable, so that the anonymous function can be called through this variable.

Method 3: Event registration form

文档对象.事件 = function(){ 函数体; }

The description is as follows:

1) When defining a named function, the function name must be specified .

2) Function name: It can be defined arbitrarily, but it must comply with the identifier naming convention, and JavaScript reserved words and keywords cannot be used. Function names generally have the first letter lowercase and are usually gerunds. It is best to know the meaning by seeing the name. If the function name consists of multiple words, use underscores to connect the words, such as get_name, or write them in camel case, such as getName.

3) Parameter list: optional. It is more than 0 parameters enclosed in parentheses and is used to receive the parameters passed by the calling function. When there are no parameters, the parentheses cannot be omitted; if there are multiple parameters, the parameters should be separated by commas. The parameter at this time is a variable without a specific value, so it is called a virtual parameter or formal parameter. Virtual parameters have no allocated storage space in memory. When passing parameters, virtual parameters can accept any type of data.

4) Function body: A block of statements enclosed by braces {}, used to implement function functions. The function body statements are executed when the function is called.

5) return[expression]: optional. Executing this statement will stop execution of the function and return the value of the specified expression. The expression can be any expression, variable or constant. Without a return statement or default expression, the function returns an undefined value.

6) Anonymous functions defined in event registration form usually do not require a return statement.

When a function needs to be called in multiple places, it needs to be defined as a named function or function expression. When it is only used to handle an event of an object, an anonymous function in the form of event registration definition is usually used.

It should be noted that from the introduction of variable promotion, we know that the scope of the famous function can be raised to the front, so the famous function can be used before definition, while the function expression must be before definition It can be used later.

Example 1:

Example 2:

Example 3:

【Related recommendations:javascript learning tutorial

The above is the detailed content of How to define a javascript function. 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