The translator needs euro amounts for email replies and amounts with two decimal places and no currency symbol for her bookkeeping sheet. Passing a formatter into the quote line function lets both outputs share the pricing rules. Functions are values in JavaScript exactly as in Python, and JavaScript code leans on that far more than Python code does.
Here’s how her formatter code maps across:
| Python | JavaScript |
|---|---|
def quote_line(client, words, fmt=for_email): |
function quoteLine(client, words, format = forEmail) { |
quote_line("Nordwind GmbH", 9400, fmt=for_sheet) |
quoteLine("Nordwind GmbH", 9400, forSheet) |
lambda amount: ... |
(amount) => ... |
Pass the Formatter
The format parameter holds a function, and format(fee) calls it. Pass forSheet, without parentheses, by the same rule as Python’s key=len. Here you pass it by position, and you’ll learn to pass options by name later in the course.
A function that another function receives and calls is a callback. In Python, you’d usually define for_sheet with def and pass its name, keeping lambdas for small jobs like key=. JavaScript code passes callbacks far more often, to array methods, timers, and event handlers, which you’ll meet later. An arrow is a complete function, so for a one-off callback you’d usually write it inline, right where you pass it.