JavaScript Function Definitions
JavaScript uses the keyword function to define a function.
A function can be defined by a declaration, or it can be an expression.
Function declaration
In previous tutorials, you have learned the syntax of a function declaration:
function functionName(parameters) {
执行的代码
}
A function declaration is not executed immediately; it is called when we need it.
Example
function myFunction(a, b) {
return a * b;
}
Try it »
|
| A semicolon is used to separate executable JavaScript statements.
Since a function declaration is not an executable statement, it does not end with a semicolon. | | --- | --- |
Function Expressions
A JavaScript function can be defined by an expression.
A function expression can be stored in a variable:
Example
var x = function (a, b) {return a * b};
Try it »
After a function expression is stored in a variable, the variable can also be used as a function:
Example
var x = function (a, b) {return a * b};
var z = x(4, 3);
Try it »
The above function is actually an Anonymous functions (the function has no name).
A function stored in a variable does not need a function name; it is usually called through the variable name.
![]() |
The above function ends with a semicolon because it is an executable statement. |
|---|
The Function() Constructor
In the above example, we learned that a function is defined with the keyword function to define a function.
A function can also be defined through the built-in JavaScript function constructor (Function()).
Example
var myFunction = new Function("a", "b", "return a * b");
var x = myFunction(4, 3);
Try it »
In fact, you do not have to use the constructor. The above example can be written as:
Example
var myFunction = function (a, b) {return a * b};
var x = myFunction(4, 3);
Try it »
![]() |
In JavaScript, many times you need to avoid using new The const keyword. |
|---|
Function Hoisting
In previous tutorials we have already learned about "hoisting".
Hoisting is JavaScript's default behavior of moving the current scope to the front.
Hoisting applies to variable declarations and function declarations.
Therefore, a function can be called before it is declared:
myFunction(5);
function myFunction(y) {
return y * y;
}
Functions defined using expressions cannot be hoisted.
Self-Invoking Functions
A function expression can "self-invoke".
A self-invoking expression is called automatically.
If an expression is immediately followed by (), it is called automatically.
A declared function cannot self-invoke.
By adding parentheses, you indicate that it is a function expression:
Example
(function () {
var x = "Hello!!"; // I will call myself
})();
Try it »
The above function is actually an An anonymous self-invoking function (has no function name).
A Function Can Be Used as a Value
A JavaScript function used as a value:
Example
function myFunction(a, b) {
return a * b;
}
var x = myFunction(4, 3);
Try it »
A JavaScript function can be used as an expression:
Example
function myFunction(a, b) {
return a * b;
}
var x = myFunction(4, 3) * 2;
Try it »
Functions Are Objects
In JavaScript, using the typeof operator to determine the function type will return "function".
But it is more accurate to describe a JavaScript function as an object.
JavaScript functions have property and the ** method**.
The arguments.length property returns the number of arguments received during a function call:
Example
function myFunction(a, b) {
return arguments.length;
}
Try it »
The toString() method returns the function as a string:
Example
function myFunction(a, b) {
return a * b;
}
var txt = myFunction.toString();
Try it »
Arrow Functions
ES6 introduced arrow functions.
The syntax of an arrow function expression is more concise than that of a regular function expression.
(参数1, 参数2, …, 参数N) => { 函数声明 }
(参数1, 参数2, …, 参数N) => 表达式(单一)
// Equivalent to: (param1, param2, ..., paramN) => { return expression; }
When there is only one parameter, the parentheses are optional:
(单一参数) => {函数声明}
单一参数 => {函数声明}
A function with no parameters should be written as a pair of parentheses:
() => {函数声明}
Example
//
ES5
var
x
=
function
(
x
,
y
)
{
return
x
y
;
}
//
ES6
const
x
=
(
x
,
y
)
=>
x
y
;
Try it »
Some arrow functions do not have their own this. They are not suitable for defining a method of an object。
When we use an arrow function, it automatically binds the value of the outer this, so the value of this inside the arrow function is the same as the outer this.
Arrow functions cannot be hoisted, so they need to be defined before use.
Use const than using var is safer, because a function expression is always a constant.
If the function body is just a single statement, you can omit the return keyword and the braces {}; this is a good practice:
Example
const
x
=
(
x
,
y
)
=>
{
return
x
y
}
;
Try it »
**Note:** IE11 and earlier IE versions do not support arrow functions.