Function

Function

Function

Function

function is used to encapsulate a piece of reusable logic. BT's function body, arrow function code block, if code block and ordinary {} code block all have return values: when there is no explicit return, the value of the last statement is the return value.

Syntax

Parameters

  • name: function name. Function declarations must have names.
  • param: parameter name. Multiple parameters are separated by commas.
  • default_value: Default parameter value. If no corresponding parameters are passed in when calling, the default value will be used.

Return Value

Fn: function value. After the function is called, it returns the value of the last statement in the function body; when using return, the current function will be ended immediately and the specified value will be returned.

Function declaration

Function declaration is suitable for defining logic that will be called multiple times.

The function body does not need to write return specifically to return the result. The return values of the following two functions are the same:

Default parameters The

parameters can set default values.

Anonymous function

Anonymous functions have no function names and can be assigned to variables or passed directly as callback parameters.

Anonymous functions also support multiple parameters and default parameters.

Arrow function

Arrow function is suitable for writing short callbacks. It is essentially a function value and can be passed as a parameter to map, filter, each, object methods, etc. where callbacks are required.

Single parameter expression

The single parameter does not need to be written in parentheses. When the right side of the arrow is an expression, the value of the expression is the return value.

undefined can also be written on the right side of the arrow in the single-parameter code block

. Multiple statements can be written in the code block, and the last statement is the return value.

Multi-parameter expression

Multiple parameters must be wrapped with (). The common parameter for array callback is value, index, current, and the common parameter for object callback is value, key, current.

Multi-parameter code block

Multi-parameter arrow functions also support {}.

Parameterless arrow function

uses empty brackets when there are no parameters.

Default parameter arrow function

Arrow function parameters with parentheses can also set default values.

Calling method The

function can be called in many ways.

return

return will end the current function immediately. Suitable for early return after hitting a certain condition.

Notes

  • Use fn name(...) { ... } for normal function declarations.
- Anonymous function uses fn(...) { ... }.

- A single-argument arrow function can be written as value -> value * 2 or value -> { value * 2 }.

- Multi-parameter arrow functions need to be written as (value, key) -> value or (value, key) -> { value }.

  • The {} code block will return the value of the last statement, and there is no need to write additional return.
  • return can only be used to end the current function early and is not recommended for abuse in simple expression callbacks.