JavaScript function definition



JavaScript uses the keyword function to define functions.

A function can be defined through a declaration or an expression.


Function declaration

In the previous tutorial, you have already learned the syntax of function declaration:

function functionName( parameters) {
Executed code
}

The function will not be executed immediately after it is declared, but will be called when we need it .

Instance

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>本例调用的函数会执行一个计算,然后返回结果:</p>
<p id="demo"></p>
<script>
function myFunction(a,b){
	return a*b;
}
document.getElementById("demo").innerHTML=myFunction(4,3);
</script>

</body>
</html>

Run instance»

Click the "Run instance" button to view the online instance

Note# Semicolons are used to separate executable JavaScript statements.
Since the function declaration is not an executable statement, it does not end with a semicolon.

Function expression

JavaScript functions can be defined by an expression.

Function expressions can be stored in variables:

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p>函数可以存储在变量中:</p>
<p id="demo"></p>
<script>
var x = function (a, b) {return a * b};
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Run Example»

Click the "Run Example" button to view the online example

After the function expression is stored in the variable, the variable can also be used as a function:

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p>函数存储在变量后,变量可作为函数使用:</p>
<p id="demo"></p>
<script>
var x = function (a, b) {return a * b};
document.getElementById("demo").innerHTML = x(4, 3);
</script>

</body>
</html>

Run instance»

Click the "Run instance" button to view the online instance

The above function is actually an anonymous function (function has no name).

Function is stored in a variable and does not require a function name. It is usually called through the variable name.

NoteThe above function ends with a semicolon because it is an execution statement.

Function() constructor

In the above example, we learned that the function is defined through the keyword function.

Functions can also be defined through the built-in JavaScript function constructor (Function()).

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p>JavaScrip 内置构造函数。</p>
<p id="demo"></p>
<script>
var myFunction = new Function("a", "b", "return a * b");
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

Actually, you don't have to use a constructor. The above example can be written as:

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p id="demo"></p>
<script>
var myFunction = function (a, b) {return a * b}
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>

</body>
</html>

Run Example»

Click the "Run Example" button to view the online example

NoteIn JavaScript, many times, you need to avoid using the new keyword.

Function Hoisting (Hoisting)

We have already learned about "hoisting (hoisting)" in the previous tutorial.

Hoisting (Hoisting) is JavaScript’s default behavior of hoisting the current scope to the front.

Hoisting is applied to variable declarations and function declarations.

Therefore, the function can be called before declaration:

myFunction(5);

function myFunction(y) {
return y * y;
}

Cannot be promoted when using an expression to define a function.


Self-calling function

Function expressions can "self-call".

Self-calling expressions will be called automatically.

If the expression is followed by (), it will be called automatically.

The declared function cannot be called by itself.

Indicate that it is a function expression by adding parentheses:

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p>函数可以自动调用:</p>
<p id="demo"></p>
<script>
(function () {
    document.getElementById("demo").innerHTML = "Hello! 我是自己调用的";
})();
</script>

</body>
</html>

Run Example»

Click the "Run Instance" button to view the online instance

The above function is actually an anonymous self-calling function (without a function name).


Function can be used as a value

JavaScript function can be used as a value:

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p>函数可作为一个值:</p>
<p>x = myFunction(4,3) 或 x = 12</p>
<p>两种情况下,x 的值都为 12。</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
    return a * b;
}
var x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Run Example»

Click the "Run Example" button to view the online example

JavaScript functions can be used as expressions:

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p>函数可作为一个表达式使用。</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
    return a * b;
}
var x = myFunction(4, 3) * 2;
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Run instance»

Click the "Run instance" button to view the online instance


Functions are objects

Using the typeof operator in JavaScript to determine the function type will return "function".

But it is more accurate to describe a JavaScript function as an object.

JavaScript functions have properties and methods.

arguments.length property returns the number of parameters received by the function calling process:

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p> arguments.length 属性返回函数接收到参数的个数:</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
    return arguments.length;
}
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>

</body>
</html>

Running instance»

Click the "Run Instance" button to view the online instance

The toString() method returns the function as a string:

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>

<p> toString() 将函数作为一个字符串返回:</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
    return a * b;
}
document.getElementById("demo").innerHTML = myFunction.toString();
</script>

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

Note# Functions are defined as properties of objects and are called object methods.
If the function is used to create a new object, it is called the constructor of the object.