亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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>函數(shù)可以存儲(chǔ)在變量中:</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>函數(shù)存儲(chǔ)在變量后,變量可作為函數(shù)使用:</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 內(nèi)置構(gòu)造函數(shù)。</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 屬性返回函數(shù)接收到參數(shù)的個(gè)數(shù):</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() 將函數(shù)作為一個(gè)字符串返回:</p> <p id="demo"></p> <script> function myFunction(a, b) { return a * b; } document.getElementById("demo").innerHTML = myFunction.toString(); </script> </body> </html>
submitReset Code