JavaScript function definition

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 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.

Function expression

JavaScript functions can be defined by an expression.

Function expressions can be stored in variables:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</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>

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

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</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>

The above function actually Is 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.

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

Function() Constructor

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

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

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</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>

Function Hoisting (Hoisting)

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

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

Hoisting is applied to variable declarations and function declarations.

Therefore, a function can be called before it is declared:

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

Hoisting is not possible when defining a function using an expression.

Self-calling function

Function expressions can be "self-calling".

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.

Function is an object

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:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</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>

The function is defined as an attribute of the object, which is called an object method.
If the function is used to create a new object, it is called the constructor of the object.

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </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>
submitReset Code