- Hands-On Functional Programming with TypeScript
- Remo H. Jansen
- 292字
- 2021-07-02 14:03:12
Function declarations and function expressions
In the preceding section, we introduced the possibility of declaring functions with (a named function) or without (an unnamed or anonymous function) explicitly indicating their name, but we didn't mention that we were also using two different types of function.
In the following example, the named function, greetNamed, is a function declaration while greetUnnamed is a function expression. For the time being, please ignore the first two lines, which contain two console.log statements:
console.log(greetNamed("John")); // OK
console.log(greetUnnamed("John")); // Error
function greetNamed(name: string): string {
return 'Hi! ${name}';
}
let greetUnnamed = function(name: string): string {
return 'Hi! ${name}';
};
We might think that the preceding functions are identical, but they behave differently. The JavaScript interpreter can evaluate a function declaration as it is being parsed. On the other hand, the function expression is part of an assignment and will not be evaluated until the assignment has been completed.
The primary cause of the different behavior of these functions is a process known as variable hoisting. We will learn more about the variable hoisting process in the Function scope and hoisting section later in this chapter.
Fortunately, the TypeScript compiler can detect this error and throw a compilation-time error. However, if we compile the preceding TypeScript code snippet into JavaScript, ignore the compilation errors, and try to execute it in a web browser, we will observe that the first console.log call works. This is the case because JavaScript knows about the declaration function and can parse it before the program is executed.
However, the second alert statement will throw an exception, to indicate that greetUnnamed is not a function. The exception is thrown because the greetUnnamed assignment must be completed before the function can be evaluated.
- Java語言程序設(shè)計
- 新編Visual Basic程序設(shè)計上機實驗教程
- LabVIEW程序設(shè)計基礎(chǔ)與應用
- Go語言高效編程:原理、可觀測性與優(yōu)化
- 深入淺出Prometheus:原理、應用、源碼與拓展詳解
- Implementing Cisco Networking Solutions
- QGIS:Becoming a GIS Power User
- 自然語言處理Python進階
- 輕松上手2D游戲開發(fā):Unity入門
- SQL Server 入門很輕松(微課超值版)
- Python網(wǎng)絡(luò)爬蟲實例教程(視頻講解版)
- MATLAB 2020 GUI程序設(shè)計從入門到精通
- Splunk Developer's Guide(Second Edition)
- Qt編程快速入門
- Python3從入門到實戰(zhàn)