- 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}';
}
- INSTANT Mock Testing with PowerMock
- C語言程序設計基礎與實驗指導
- 大模型RAG實戰:RAG原理、應用與系統構建
- 深入分布式緩存:從原理到實踐
- Protocol-Oriented Programming with Swift
- Maker基地嘉年華:玩轉樂動魔盒學Scratch
- Mastering Apache Storm
- Python開發基礎
- Java Web開發實例大全(基礎卷) (軟件工程師開發大系)
- App Inventor 2 Essentials
- HTML+CSS+JavaScript網頁制作:從入門到精通(第4版)
- C語言從入門到精通
- Dart:Scalable Application Development
- Spring Boot 3:入門與應用實戰
- WCF編程(第2版)