- Hands-On Functional Programming with TypeScript
- Remo H. Jansen
- 232字
- 2021-07-02 14:03:13
Trailing commas in function arguments
Trailing commas are commas that are used after the final argument of a function. Using a comma after the last parameter of a function can be useful because it is very common to forget a comma when we modify an existing function by adding additional parameters.
For example, the following function only takes one parameter and doesn't use trailing commas:
function greetWithoutTralingCommas(
name: string
): string {
return 'Hi! ${name}';
}
Some time after the initial implementation, we might be required to add a parameter to the previous function. A common mistake is to declare the new parameter and forget to add a comma after the first parameter:
function updatedGreetWithoutTralingCommas(
name: string
surname: string, // Error
): string {
return 'Hi! ${name} ${surname}';
}
Using a trailing comma in the first version of the function could have helped us to prevent this common mistake:
function greetWithTralingCommas(
name: string,
): string {
return 'Hi! ${name}';
}
Using a trailing comma eliminates the possibility of forgetting the comma when adding a new argument:
function updatedGreetWithTralingCommas(
name: string,
surname: string,
): string {
return 'Hi! ${name} ${surname}';
}
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- The Complete Rust Programming Reference Guide
- Hyper-V 2016 Best Practices
- vSphere High Performance Cookbook
- Learning Data Mining with Python
- Java從入門到精通(第5版)
- Learn Scala Programming
- Quarkus實踐指南:構建新一代的Kubernetes原生Java微服務
- Getting Started with Greenplum for Big Data Analytics
- 零基礎入門學習Python(第2版)
- HTML5 APP開發從入門到精通(微課精編版)
- HTML 5與CSS 3權威指南(第3版·上冊)
- 一塊面包板玩轉Arduino編程
- SwiftUI極簡開發
- 嵌入式Linux C語言程序設計基礎教程