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
fn name(param1, param2 = default_value) { statement result } fn(param1, param2) { result } value -> value * 2 (value, key) -> { value + ':' + key }
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.
fn add(a, b) { a + b } sum = add(1, 2)
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:
fn add(a, b) { a + b } fn add_by_return(a, b) { return a + b }
Default parameters The
parameters can set default values.
fn hello(name = 'BT') { 'Hello ' + name } a = hello() b = hello('Tom')
Anonymous function
Anonymous functions have no function names and can be assigned to variables or passed directly as callback parameters.
double = fn(value) { value * 2 } a = double(3) b = [1, 2, 3].map(fn(value) { value * 2 })
Anonymous functions also support multiple parameters and default parameters.
join = fn(left, right = 'BT') { left + right } result = join('Hello ', 'World')
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.
nums = [1, 2, 3] result = nums.map(value -> value * 2)
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.
nums = [1, 2, 3] result = nums.map(value -> { next = value * 2 next + 1 })
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.
nums = [10, 20, 30] result = nums.map((value, index) -> value + index) user = {name: 'BT', age: 1} result = user.map((value, key) -> key + '=' + string(value))
Multi-parameter code block
Multi-parameter arrow functions also support {}.
user = {name: 'BT', age: 1} result = user.map((value, key) -> { prefix = key + ':' prefix + string(value) })
Parameterless arrow function
uses empty brackets when there are no parameters.
create_name = () -> 'BT' name = create_name()
Default parameter arrow function
Arrow function parameters with parentheses can also set default values.
format = (name, prefix = 'Hello ') -> prefix + name text = format('BT')
Calling method The
function can be called in many ways.
fn add(a, b) { a + b } // Call a = add(1, 2) by function name // Call sum = add b = sum(3, 4) after the function is assigned to a variable // Call double = fn(value) { value * 2 } c = double(5) after the anonymous function is assigned immediately // Call triple = value -> value * 3 d = triple(6) after the arrow function is assigned // Pass in items = [1, 2, 3] e = items.map((value, index) -> value + index) as a callback // Use call to dynamically call f = call(add, 7, 8)
return
return will end the current function immediately. Suitable for early return after hitting a certain condition.
fn check(user) { if !user { return ' Not logged in ' } ' Logged in ' }
Notes
- Use
fn name(...) { ... }for normal function declarations.
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 additionalreturn. -
returncan only be used to end the current function early and is not recommended for abuse in simple expression callbacks.